Aborting a stash pop in Git
My use case: just tried popping onto the wrong branch and got conflicts. All I need is to undo the pop but keep it in the stash list so I can pop it out on the correct branch. I did this:
git reset HEAD --hard
git checkout my_correct_branch
git stash pop
Easy.
Simple one-liner
I have always used
git reset --merge
I can't remember it ever failing.
Ok, I think I have worked out "git stash unapply". It's more complex than git apply --reverse
because you need reverse merging action in case there was any merging done by the git stash apply
.
The reverse merge requires that all current changes be pushed into the index:
git add -u
Then invert the merge-recursive
that was done by git stash apply
:
git merge-recursive stash@{0}: -- $(git write-tree) stash@{0}^1
Now you will be left with just the non-stash changes. They will be in the index. You can use git reset
to unstage your changes if you like.
Given that your original git stash apply
failed I assume the reverse might also fail since some of the things it wants to undo did not get done.
Here's an example showing how the working copy (via git status
) ends up clean again:
$ git status
# On branch trunk
nothing to commit (working directory clean)
$ git stash apply
Auto-merging foo.c
# On branch trunk
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: foo.c
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git add -u
$ git merge-recursive stash@{0}: -- $(git write-tree) stash@{0}^1
Auto-merging foo.c
$ git status
# On branch trunk
nothing to commit (working directory clean)