Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find last git commit before a merge

Tags:

After I have done a merge to my master branch from a working branch with git, I sometimes want to find to find the last commit on master before the merge happened. How do I do this?

like image 273
Obromios Avatar asked Jan 13 '17 06:01

Obromios


People also ask

How can I see my last commit in git?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How do you check commit is merge commit?

To see the merge commit's message and other details, use git show-merge with the same arguments.

How do you look at a previous commit?

If you want to look at previous commits, you can use git log and its many arguments. If you want to checkout an actual commit to view the files in an editor, just use git checkout to move to any commit you want. When you are finished, just do git checkout master to return to your current state.

How can I see last merge?

Try simply git whatchanged master.. staging to see what changed on staging since you last merged from staging to master.


2 Answers

The quick way to determine commit after merge occured is to use the reflog.

Assuming that last occured operation was a merge, then:

git log HEAD@{1} -1 

HEAD@{1} refers to the previous HEAD before the last operation, so you can address it using log and reflog.

git log will show you sequence of the commits in the current branch, so after a merge it always will be a merge commit, and right before it will be commits from the merged branch. git reflog shows the sequence of operations in your repository (for example merge, rebase). As explained in the docs:

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository. Reflogs are useful in various Git commands, to specify the old value of a reference.

like image 179
2oppin Avatar answered Sep 22 '22 22:09

2oppin


A quick way to do this is to type

git log --pretty=format:'%h : %s' --graph 

then just follow down the graph on the right hand side till you find the merge point. You can also do

git log --pretty=format:'%h : %s' --graph > temp.txt 

which puts the output into a file, temp.txt, that which you can open in your editor and use the search facility to look for text like merge.

This approach is useful to answer lots of other questions about the lineage of your latest commit so have put

alias git_graph="git log --pretty=format:'%h : %s' --graph" 

in my .bash_profile file so I can just use ```git_log`` to see this information.

like image 38
Obromios Avatar answered Sep 21 '22 22:09

Obromios