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?
-
1You may try `git log -1`, it would give details of last commit of the selected branch. Similarly, `git log -2` would give details of last 2 commits. – Abhi Jan 13 '17 at 06:53
-
Also try using gitk to visualise the structure, and to check any command scripts, just in case they don't get the right merge-base (fork-point). – Philip Oakley Jan 13 '17 at 15:21
10 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.
-
4When I tried this, it gave the last commit on the branch that was merged, not the last commit on master before the branch was merged. – Obromios Jan 14 '17 at 00:42
-
@Obromios, what git reflog --oneline shows? what operations in the list? You should select the next before the merge(i.e. if HEAD@{0} -- merge, then HEAD@{1}), I think merge was not the last operation, – 2oppin Jan 14 '17 at 08:11
-
just a caveat. it will give incorrect results if your main local branch was not updated from long time. – Manish Bansal Mar 21 '22 at 07:39
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.

- 15,408
- 15
- 72
- 127
-
1
-
@mallaudin: I believe Obromios is referring to a situation in which the merge is several or even many commits *behind* the tip of `master`. That is, `git log -1` will show only one non-merge commit. – torek Jan 13 '17 at 13:43
-
-
1This is waaaay to much information to parse through, most people with this question just want what is described in 2oppin's answer. – ldog Jun 26 '19 at 21:31
-
Instead of '*... --graph > temp.txt*' you can use '*git log ... --graph | less*' and press slash `/` to search for a phrase (don't need to create any file or play with editors). – bloody Mar 26 '20 at 11:35
-
If you have already merged branch
into master
, here is one way to find the 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 state of master
before the merge is this commit's first parent :
$ h=`git rev-list --ancestry-path branch..master | tail -1`
$ git log $h^

- 46,477
- 5
- 57
- 104
-
I checked this and it works, thank you. However I will be accepting my answer, stackoverflow.com/a/41628915/1299362, as it is more generally useful. I could see myself using your answer if the merge was a long, long way back. – Obromios Jan 14 '17 at 21:43
Here's a programmatic solution. We query for the following to get the hash of the previous master commit before the merge happened:
git merge-base master master~1
If the last PR is a merge, then master~1
belongs to the PR. merge-base
get the common ancestor's SHA which will be the previous master commit due to how PR is structured.
If the last PR is a squash, then master~1
is on master and is what we want. Since master~1
is a parent commit of master
this time, git merge-base master master~1
get the common ancestor and so correctly return the SHA of master~1
.
With the SHA, we can get other details of the commit with git log
etc.
Observe that this might not give us the last master commit if there are many PR merges without firstly rebasing to latest master. However, this is consistent with what the OP wanted, which is last master commit before merge.

- 301
- 1
- 6
To get just the merge point commit id:
git rev-list origin..HEAD --max-parents=1 --max-count=1

- 41
- 4
Although you've already picked an answer (and probably become more familiar with git in the 5 years since asking this), I thought I'd add an answer that isn't here yet.
If you've just merged, the quick and dirty way is to git show
, and the answer will be the first (abcdefg
) value in Merge: abcdefg 1234567
.
The canonical way of finding the parent to the current head that is a child of the branch that was merged into is git rev-parse HEAD^
(^
=^1
, the first parent). For example if you git checkout master
, then git merge other-branch
, then git rev-parse HEAD^
will be the previous HEAD
of master
. (Where git rev-parse HEAD^
, git show HEAD^
, and git log HEAD^ -1
are equivalent in using the context HEAD^
).
This can be extended ~ i.e. if you want to see only the history of the master branch, the logical extension is HEAD^
, HEAD^^
, HEAD^^^
, ..., which can be achieved by adding --first-parent
to a git log
~ so if you simply want to see the order of previous HEAD
's of master
, then you can
git log master --oneline --first-parent --pretty=format:'%H : %s'
I personally find these useful as an alias
alias mainline="git log master --oneline --first-parent --pretty=format:'%H : %T'"
alias maindiff="git diff $(git rev-parse master^1)..$(git rev-parse master) --stat"

- 724
- 1
- 4
- 15
If you know the order of the merge commits, you could 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 if the pretty-format is not oneline, email or raw, an additional line is inserted before the Author: line. This line begins with "Merge: " and the sha1s of ancestral commits are printed, separated by spaces.

- 368
- 1
- 3
- 6
Was looking into the same and here's what I came up with:
git log --merges -1 --format='%p' | awk '{print $2}'
Explanation:
--merges
– show only merge commits (those with min parents of 2)-1
– show only last log line--format='%p'
– show abbreviated parent commit hashes

- 126
- 3
Last commit SHA of branch that got merged in current one:
git rev-list HEAD^1..HEAD --max-parents=1 --max-count=1

- 99
- 1
- 5