Search code examples
gitgit-checkoutgit-stash

Using git stash, checkout previous commit, and going back


I wanted to go back to a commit to see how things were at that moment, so I did

git stash

... to save my changes then I used

git checkout <hash>

When done with my exploration, I just wanted to go back where I was and I used

git stash apply

Now files are messed up and I can't see further commits when using git log

What's going on? How do I get back to where I was?

EDIT

Tried to do a git checkout master but had some conflicts (a README.md, marking "deleted by US")

So I added it, stashed my changes, used git checkout master and then applied the stash.

Fixed the README.md conflict and everythings's fine.

GIT hurts my brain.


Solution

  • First, you need to undo the mess you made with git stash apply. You can do this with git reset --hard HEAD. (Warning: this will throw away all local changes.) Then you need to check out your previous branch. If you have not done any other git checkout commands, you can use git checkout @{-1} to checkout the previous branch. Finally, you can git stash apply to reapply your stash exactly where you were working before all this started.