0

I wanted to check a previous commit.

I am in brach mybranch where I had a few changes in a file, that's why I did git stash first.

Then,

git checkout previouscommit

I ran the code, made a few changes that I don't want to save.

Now, how can I go back to mybranch?

1) I don't want to keep the changes I made in previouscommit.

2) I want to go back to mybranch

I know I have to do a git stash pop but I am not sure at what point. Before, or after checking mybranch? (Also I can't right now because I have done some changes as I said to a file that I don't want to keep and I am not sure how to force to go back to mybranch.)

Mischa
  • 623
  • 5
  • 17
George
  • 5,808
  • 15
  • 83
  • 160

4 Answers4

3

You can use the following sequence of commands:

git checkout .
git checkout -
git stash pop

The first one throws out the changes you've made on previouscommit, the second returns you to the previously checked out commit/branch, the last one restores the changes you've stashed.

You can use git checkout mybranch instead of the second command.

TerraPass
  • 1,562
  • 11
  • 21
  • I noticed one thing though..At the `mybranch` the changes I have made to one file aren't there..Weird..I have done first `git stash` and I followed your directions. – George Jun 16 '16 at 11:13
  • 1
    @George You've done `git stash` *before* you've checked out the `previouscommit`, correct? In this case the changes you made to tracked files at `mybranch` should all be in the stash and be re-applied once you do `git stash pop`. It's really strange if that's not the case. – TerraPass Jun 16 '16 at 11:25
  • :yes,I did first `git stash` , then checkout to previouscommit.Anyway,thankfully I remember the changes I had done but very weird! – George Jun 16 '16 at 11:28
0
  • At first, if you don't want to keep the latest changes you can revert them by: git reset --hard
  • Then you can checkout your branch using git checkout mybranch
  • And then you need to do git stash pop
Mischa
  • 623
  • 5
  • 17
Armine
  • 1,675
  • 2
  • 24
  • 40
0

I hope this help someone. I used git stash and had a hard time going back to the point where i was before using that command.

1- I used git stash list it retrieved the last commit code and message before the stash command.

2- I grabbed the retrieved code and used git checkout <commit code>.

-1

Just do a

git reset --hard mybranch
git stash pop
Mischa
  • 623
  • 5
  • 17