How to find the last git commit before merging

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

+12
git
source share
6 answers

A quick way to do this is to enter

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

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

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

which puts the output in a temp.txt file that you can open in your editor, and use a search tool to search for text such as merge.

This approach is useful for answering many other questions about the origin of your last commit, so they 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.

+3
source share

A quick way to determine a commit after a merge is to use reflog .

Assuming the last operation that occurred was a merger, then:

 git log HEAD@ {1} -1 

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

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

Link logs or "reflogs", a record of when branch tips and other links have been updated in the local repository. Reflogs are useful in various Git commands to indicate the old value of a link.

+7
source share

If you already merged branch into master , here is one way to find a merge commit:

 # With the ancestry-path option, 'git rev-list branch..master' (or git log) # will only list commits which are parents of 'master' # *and* children of 'branch' # # If 'branch' was merged into 'master', the last commit in this list # is the merge commit : $ git rev-list --ancestry-path branch..master | tail -1 

The master state before merging is the first parent commit:

 $ h=`git rev-list --ancestry-path branch..master | tail -1` $ git log $h^ 
+3
source share

git log -1

Also see git log --help or https://git-scm.com/docs/git-log

+1
source share

If you know the merge fix order, you can use the following:

 $ git log -n 1 --pretty | grep Merge | head -1 | awk '{print $2}' 

https://git-scm.com/docs/git-log

If the commit is a merge and the pretty format is not oneline, email or raw, an additional line is inserted before the line Author: This line starts with "Merge:" and prints the hereditary commit shades separated by spaces.

0
source share

To get only the merge point commit id:

 git rev-list origin..HEAD --max-parents=1 --max-count=1 
0
source share

All Articles