如何在弹出冲突的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
相关产品推荐
相关产品推荐

