George
George

Reputation: 5681

Go back to original branch after git stash

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.)

Upvotes: 0

Views: 1079

Answers (4)

Marcio Andre
Marcio Andre

Reputation: 11

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>.

Upvotes: 0

Armine
Armine

Reputation: 1695

  • 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

Upvotes: 0

Mischa
Mischa

Reputation: 641

Just do a

git reset --hard mybranch
git stash pop

Upvotes: -1

TerraPass
TerraPass

Reputation: 1602

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.

Upvotes: 3

Related Questions