Cerin
Cerin

Reputation: 64830

How to checkout git state before merge?

I merged a git branch into my master, but now I want to get the state of the code before the merge. I do not want to revert the merge. How do I do this? Running git log shows hundreds of commits, but doesn't list the branch, so I'm not sure how to find the commit hash. This answer recommends running git log master.. but this returns nothing for me.

Upvotes: 7

Views: 6013

Answers (4)

Edson Horacio Junior
Edson Horacio Junior

Reputation: 3143

Use git reflog to see the history, you'll see something like this:

72387432 (HEAD -> master, origin/master, origin/HEAD) HEAD@{4}: pull: Fast-forward
3df12c55 HEAD@{5}: checkout: moving from dev to master

The merge happened at 72387432, I know this because of the pull (in my case the merge came from a git pull, after merging the PR in bitbucket). So what you want is to checkout right before that which is 3df12c55:

git checkout 3df12c55

Now you can check the code, do tests, etc. When you want to get back to master you can just run git checkout master.

Upvotes: 1

Raman Sharma
Raman Sharma

Reputation: 2170

If you have not deleted your merged branch yet. Then you can use

git log branchMerged..master

It will list down all the commits which are present in master and not present in branch which you merged.

Here branchMerged : Name of the branch which you have merged. And I assume you have merged in master.

Upvotes: 0

oginski
oginski

Reputation: 364

Try this command to find hash (HEAD):

git reflog

Upvotes: 1

Ry-
Ry-

Reputation: 225144

If the merge commit is HEAD, the commits that were merged are HEAD^1 and HEAD^2 (and so on, if there are more than two parents). Check it out!

git checkout master^2  # probably the branch merged in
git checkout master^1  # probably master before the merge

Upvotes: 4

Related Questions