0

I have gotten in the habit of using the git stash/git stash pop feature as I get random requests to make fixes at work while I'm in the middle of doing something on a different branch.

This morning I decided to start with a project I was working on on a different branch. I didn't remember if anything was stashed or not so I called git stash pop. Inexplicably, I called it twice more. I have no idea what I did, but I want to undo it. What should I do exactly?

Thanks!

Nick Manning
  • 2,828
  • 1
  • 29
  • 50

2 Answers2

1

Running git stash pop only changes your working directory; it does not commit any changes to your repository. You can restore the state of your working directory to the state it was in before running git stash pop by running:

git reset --hard HEAD

This reverts your working directory to the current HEAD of your repository. You will have lost your stashes, though.

From a workflow perspective, don't be afraid to just commit your changes to your branch rather than using git stash: you can always edit the commit before you share it with anybody.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Also! If you are nervous about trying random commands suggested by people on stackoverflow, remember that you can always make a copy of your repository to test with (`cp -a myrepo myrepo-for-testing`). – larsks Sep 28 '17 at 16:05
  • **NOTE : you will loose all that was stashed**. Its already mentioned in the ans, but I think it should be highlighted as, the OP stashed them because because he wanted to keep them. – zeekhuge Sep 28 '17 at 16:07
0

You have lost your stashes, and I don't think there's any easier way to bring them back. As far as cleaning your working directory is concerned, just do

git stash # again

or 

git checkout -f # if you're not interested in keeping any stash changes

As a suggestion, always prefer git stash apply over git pop

hspandher
  • 15,934
  • 2
  • 32
  • 45
  • Could `git fsck` not be used to recover the stash objects? – stwalkerster Sep 28 '17 at 16:10
  • Yes, it's possible to stash hash, however as I said there is no easier way. You may consult this answer if you're interested. https://stackoverflow.com/questions/89332/how-to-recover-a-dropped-stash-in-git – hspandher Sep 28 '17 at 16:18