Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Find the most recent common ancestor of two branches

Tags:

git

git-branch

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

like image 546
Ted Avatar asked Oct 10 '09 21:10

Ted


People also ask

How does git find common ancestors?

DESCRIPTION. 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.

What is git rebase vs merge?

Git Merge Vs Git Rebase:Git merge is a command that allows you to merge branches from Git. Git rebase is a command that allows developers to integrate changes from one branch to another. In Git Merge logs will be showing the complete history of the merging of commits.


2 Answers

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?


You are looking for git merge-base. Usage:

$ git merge-base branch2 branch3 050dc022f3a65bdc78d97e2b1ac9b595a924c3f2 
like image 143
CB Bailey Avatar answered Sep 29 '22 00:09

CB Bailey