You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在弹出冲突的Git stash时保留暂存区索引?

如何在解决Stash冲突后恢复原暂存区索引

我日常开发中会大量使用Git暂存区记录已确认的更改,工作目录则常充斥未测试的方案、TODO注释及WIP代码。一旦丢失暂存区与工作目录的区分,我就得重新评估所有更改,这会带来极大不便。

当需要先处理其他工作时,我会用git stash push暂存当前状态,等其他更改提交、工作目录清空后,再执行git stash pop --index恢复。但Stash条目与新HEAD经常存在微小冲突,导致--index参数失效,我不得不丢弃暂存区,解决冲突后手动重建,效率很低。

我想寻求一种方法,能在解决冲突后保留或恢复原来的暂存区索引:

  • 不介意暂存区的冲突是否解决,或保持Stash原样也可接受
  • 优先希望先弹出Stash、解决冲突,再恢复旧索引
  • 如果需要分别处理暂存区的冲突,我也能接受

复现问题的Shell脚本:

mkdir example && cd example || exit

git init

printf 'first line\nlast line\n' >foo
git add foo
git commit -m 'initial commit'

sed -i '2i a good line that should be staged' foo
git add foo
sed -i '3i a WIP line that should NOT be staged' foo
git stash push -m 'the stash with index'

sed -i '2i some conflicting change' foo
git commit -a -m 'a new HEAD conflicting with stash'

git stash pop --index  # 此命令执行失败,无法恢复原暂存区

内容的提问来源于stack exchange,提问作者Piotr Siupa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:05:12