72

I regret having applied a stash (wrong branch). How can I undo this and have my stash back to my stash list in order to apply it later on the right branch?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
abernier
  • 27,030
  • 20
  • 83
  • 114

1 Answers1

87

If you haven't committed, you should just be able to git stash again, possibly with a git reset HEAD first.

Also, git stash apply doesn't delete the stash like git stash pop does. So if you have committed, you could git reset --hard [last_good_commit] (if you haven't pushed) or git revert [last_good_commit] (if you have pushed) and just apply the stash again once you're on the right branch.

Note: Running git reset --hard will delete any uncommitted code.

ThinkDigital
  • 3,189
  • 4
  • 26
  • 34
Brandan
  • 14,735
  • 3
  • 56
  • 71
  • 2
    How does this undo applying the stash? – Andrew Marshall Feb 11 '12 at 18:33
  • 1
    All that `git stash apply` should do is apply some changes to files in your working space. If you don't want those changes, just stash them back away. If you had uncommitted changes in your working space, it gets messier, and there's no generic answer for that. – Brandan Feb 11 '12 at 18:37
  • 5
    Probably should note that in your answer, because otherwise a `git reset --hard` will cause those other changes to be lost forever. – Andrew Marshall Feb 11 '12 at 18:40
  • 8
    When I applied the stash, there were conflicts... So a `git stash` again left me with "foo: needs merge\n bar: unmerged" :/ – abernier Feb 11 '12 at 18:44
  • 4
    If you originally ran `git stash apply`, then that stash is still present in the repo. You can do whatever you need to do to get this branch back to a good state — revert, reset, rebase, resolve, whatever. Then later on, you can check out the correct branch and apply the original stash. But as @AndrewMarshall pointed out, you should make sure your original stash is intact first: `git stash list -p`. – Brandan Feb 11 '12 at 18:58