Undoing accidental git stash pop

GitGit Stash

Git Problem Overview


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.

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

Git Solutions


Solution 1 - Git

Try using https://stackoverflow.com/questions/89332/recover-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 show them to see the diff and use patch -R to unapply them.

Solution 2 - Git

From 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.

Solution 3 - Git

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.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnrenView Question on Stackoverflow
Solution 1 - GitBen JacksonView Answer on Stackoverflow
Solution 2 - GitkacharView Answer on Stackoverflow
Solution 3 - GitmarkusView Answer on Stackoverflow