Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undoing accidental git stash pop

Tags:

git

git-stash

People also ask

Can I git stash pop twice?

If you made two stashes, then just call git stash pop twice. As opposed to git stash apply , pop applies and removes the latest stash.

Can you undo git stash clear?

All of the above answers end with a git stash apply [commit] which is good, but is not an exact undo of git stash clear . For that you need to re-stash the orphaned stash-commit.


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 show them to see the diff and use patch -R to unapply them.


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.


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.