Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find previous merge commit

Tags:

git

How can one find the previous merge commit between two branches?

I would like to see the changes in my master branch since the last time I have merged the release branch into the master branch. To see the changes in the release branch since the last branch, it is as easy as git diff ...release

But obviously git diff release... doesn't work because it also includes all the changes before the last merge. Thus I think I need the commit id of the last merge to pass it to git diff

git log --reverse --ancestry-path `git merge-base HEAD release`.. \ 
        --format=format:%H|head -n1

seems to work and can be used with git diff $(...), but it seems awfully complicated. Is there some easier solution?

Example

  I
 / \
A1 B1
 \ |
|  M   
|  |
A2 B2

Here I is the initial commit. A[12] are the release commits and B[12] are the master commits. M is the previous commit. In the example the changes between the last merge and master is just the changes introduces by B2. git merge-base A2 B2 returns A1. And git diff B2 A1 includes the changes of B1. Thus the question is how to find M in the general more complex case so that one can run git diff M B2 without having to manually find M.

like image 612
Roland Schulz Avatar asked May 09 '12 23:05

Roland Schulz


People also ask

How can I see the last commit before merge?

You 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. 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).

How do I see a merge commit?

1 answer. You just need to check the length of the `parents`. If there are two or more commits, it's a merge commit, otherwise it's a regular commit.

Does git merge preserve history?

In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from main using either git merge or git rebase . Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of main .


1 Answers

It seems what you are looking for is the point at which the two branches started to differ, not the last merge between the two branches. These two concepts are different because your release branch may have been fast forwarded some time after it was merged.

git merge-base master release will find the most recent common ancestor of your master and release branches (i.e. the last commit that the two have in common).

Then, on master you can use git diff [common ancestor] HEAD to see the changes that have been made to master since the common ancestor.

like image 194
Jake Greene Avatar answered Sep 30 '22 21:09

Jake Greene