撤消意外的 git stash pop

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6543519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:16:45  来源:igfitidea点击:

Undoing accidental git stash pop

gitgit-stash

提问by nren

I stashed some local changes before doing a complicated merge, did the merge, then stupidly forgot to commit before running git stash pop. The pop created some problems (bad method calls in a big codebase) that are proving hard to track down. I ran git stash show, so I at least know which files were changed. If nothing else, I guess this is a lesson to commit more.

我在进行复杂的合并之前存储了一些本地更改,进行了合并,然后愚蠢地在运行之前忘记了提交git stash pop。pop 产生了一些问题(大型代码库中的错误方法调用),这些问题证明很难追踪。我跑了git stash show,所以我至少知道哪些文件被更改了。如果不出意外,我想这是一个需要付出更多的教训。

My question: is it possible to undo the stash pop without also undoing the merge?

我的问题:是否可以在不撤消合并的情况下撤消 stash pop?

采纳答案by Ben Hymanson

Try using How to recover a dropped stash in Git?to find the stash you popped. I think there are always two commits for a stash, since it preserves the index and the working copy (so often the index commit will be empty). Then git showthem to see the diff and use patch -Rto unapply them.

尝试使用如何在 Git 中恢复丢失的存储?找到你弹出的藏匿处。我认为存储总是有两次提交,因为它保留了索引和工作副本(因此索引提交通常为空)。然后git show他们查看差异并使用patch -R取消应用它们。

回答by kachar

From git stash --help

git stash --help

Recovering stashes that were cleared/dropped erroneously
   If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms. However, you can try the
   following incantation to get a list of stashes that are still in your repository, but not reachable any more:

       git fsck --unreachable |
       grep commit | cut -d\  -f3 |
       xargs git log --merges --no-walk --grep=WIP

This helped me better than the accepted answer with the same scenario.

这比在相同情况下接受的答案更好地帮助了我。

回答by markus

If your merge was not too complicated another option would be to:

如果您的合并不太复杂,另一种选择是:

  1. Move all the changes including the merge changes back to stash using "git stash"
  2. Run the merge again and commit your changes (without the changes from the dropped stash)
  3. Run a "git stash pop" which should ignore all the changes from your previous merge since the files are identical now.
  1. 使用“git stash”将所有更改(包括合并更改)移回 stash
  2. 再次运行合并并提交您的更改(没有来自删除的存储的更改)
  3. 运行“git stash pop”,它应该忽略之前合并中的所有更改,因为现在文件是相同的。

After that you are left with only the changes from the stash you dropped too early.

在那之后,你只剩下你过早丢弃的藏匿处的变化。