Git: Find the most recent common ancestor of two branches

git merge-base example
git show-branch
git set merge-base
git rebase
git find ancestor branch
git rev-list
git log
git find branch point

How to find the most recent common ancestor of two Git branches?


You are looking for git merge-base. Usage:

$ git merge-base branch2 branch3
050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

git-merge-base Documentation, git-merge-base - Find as good common ancestors as possible for a merge As the most common special case, specifying only two commits on the Find the point at which a branch (or any history that leads to <commit>) forked from another want to rebase the work you did on the topic on top of the updated origin/master. The command, git merge-base will work like a charm in this case: $ git merge-base featureBranch developBranch. Additional info: If featureBranch is your current branch, you can use use '--fork-point' as shown below: $ git merge-base --fork-point develop. Also, for full commit info you can use this command:


git diff master...feature

shows all the new commits of your current (possibly multi-commit) feature branch.

man git-diff documents that:

git diff A...B

is the same as:

git diff $(git merge-base A B) B

but the ... is easier to type and remember.

As mentioned by Dave, the special case of HEAD can be omitted. So:

git diff master...HEAD

is the same as:

git diff master...

which is enough if the current branch is feature.

Finally, remember that order matters! Doing git diff feature...master will show changes that are on master not on feature.

I wish more git commands would support that syntax, but I don't think they do. And some even have different semantics for ...: What are the differences between double-dot ".." and triple-dot "..." in Git commit ranges?

Git: Find the most recent common ancestor of two branches, The command, git merge-base will work like a charm in this case: $ git merge-​base featureBranch developBranch. Additional info: If  How do I find the most recent common ancestor commit of two git branches in Git Extensions? Is it possible?


As noted in a prior answer, git merge-base works:

$ git merge-base myfeature develop
050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

but if myfeature is the current branch, as is common, you can use use --fork-point:

$ git merge-base --fork-point develop
050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

This argument works only in sufficiently recent versions of git. Unfortunately it doesn't always work, however, and it is not clear why. Please refer to the limitations noted toward the end of this answer.


For full commit info, consider:

$ git log -1 $(git merge-base --fork-point develop) 

git-merge-base, DESCRIPTION git merge-base finds best common ancestor(s) between two This is different from git-show-branch(1) when used with the --merge-base option. $ git merge-base myfeature develop 050dc022f3a65bdc78d97e2b1ac9b595a924c3f2 but if myfeature is the current branch, as is common, you can use use --fork-point: $ git merge-base --fork-point develop 050dc022f3a65bdc78d97e2b1ac9b595a924c3f2 This argument works only in sufficiently recent versions of git.


With gitk you can view the two branches graphically:

gitk branch1 branch2

And then it's easy to find the common ancestor in the history of the two branches.

Deciphering "git show-branch" To See Branches And Their Commits , In no particular order, each line lists one branch and its most recent commit text in the The common ancestor of two branches is where the “branch” occurred. Compare two branches using git diff. In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots. $ git diff branch1..branch2. Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.


Finding a branch point with Git? - Stack Overflow, How can I find the commit in my repository when branch A was created the mix, and makes this "safer" (i.e. it returns a result (namely, the most recent git merge​-base finds best common ancestor(s) between two commits to  git merge-base B E allows to find where a the common ancestor A of the two commits. Is there a way to find the commit F where the two branches are merged again? git merge branch git-branch


Git: Find the most recent common ancestor of two branches, git diff masterfeature. shows all the new commits of your current (possibly multi-​commit) feature branch. man git-diff documents that: git diff AB. is the same as: git merge-base finds best common ancestor(s) between two commits to use in a three-way merge. One common ancestor is better than another common ancestor if the latter is an ancestor of the former. A common ancestor that does not have any better common ancestor is a best common ancestor, i.e. a merge base. Note that there can be more than one merge base for a pair of commits.


How do I find the most recent common ancestor commit of two git , How do I find the most recent common ancestor commit of two git branches in Git Extensions? Is it possible? Say I have two branches in Git: development and qa-test. Both of these branches contain different features - commits but also A is the latest common ancestor of these two branches: A--->B--->C--->developent \ \ D--->E--->qa-test