How to find last git commit before a merge

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.

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.

Leave a Comment