Motig Motig - 6 months ago 48
Git Question

Find common ancestor of more than two branches

Find common ancestor of two branches - this question explains how to show a common ancestor for two branches.

git merge-base branch1 branch2

However, I want to see a common ancestor of three or more branches. At first, I thought this would work.

git merge-base branch1 branch2 branch3 branch4

But it doesn't actually return the common ancestor of all the branches; as noted in the docs

Given three commits A, B and C,
git merge-base A B C
will compute the merge base between A and a hypothetical commit M, which is a merge between B and C

which isn't what I want.

How do you find a common ancestor commit of more than two branches?


This seems to work

git show-branch --merge-base branch1 branch2 branch3 branch4

Thanks to the comment of user twalberg, the following command is the equivalent to show-branch

git merge-base --octopus branch1 branch2 branch3 bracnh4