Obromios
Obromios

Reputation: 16383

How to find last git commit before a merge

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?

Upvotes: 31

Views: 21596

Answers (12)

Adam Wise
Adam Wise

Reputation: 2270

For anyone using Gitlab, the page any given commit indicates the "parent" of the current merge, displaying the last git commit before a merge:

Parent commit in GitLab

Upvotes: 0

Bhavya Shah
Bhavya Shah

Reputation: 1

I was having the same issue and what i figured out using terminal was: type

git log

in the terminal and keep pressing Key s until you find the commit hash you want. What pressing s key will do for you is it will fetch the commits for the branch you are checked-in in chrnological order.

Upvotes: 0

nyxgear
nyxgear

Reputation: 179

Last commit SHA of branch that got merged in current one:

git rev-list HEAD^1..HEAD --max-parents=1 --max-count=1

Upvotes: 1

Skenvy
Skenvy

Reputation: 1390

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"

Upvotes: 1

George L. Yermulnik
George L. Yermulnik

Reputation: 126

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

Upvotes: 0

ShaoWei Teo
ShaoWei Teo

Reputation: 421

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.

Upvotes: 5

O M Kadiri
O M Kadiri

Reputation: 41

To get just the merge point commit id:

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

Upvotes: 3

Valentin Agachi
Valentin Agachi

Reputation: 368

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.

Upvotes: 0

2oppin
2oppin

Reputation: 1991

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.

Upvotes: 21

Obromios
Obromios

Reputation: 16383

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.

Upvotes: 19

LeGEC
LeGEC

Reputation: 51850

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^

Upvotes: 6

UserASR
UserASR

Reputation: 2205

git log -1

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

Upvotes: 2

Related Questions