← All languages
Git command of the day
Random

merge-base

Find the best common ancestor between two commits.

Description

The git merge-base command finds the best common ancestor(s) between two commits to use in a three-way merge. The best common ancestor is the commit that is an ancestor of both commits and is not an ancestor of any other common ancestor.

One common ancestor is better than another if it is an ancestor of the other. A common ancestor that does not have any better common ancestor is a best common ancestor, or merge base.

This plumbing command is frequently used in scripts to determine the fork point between two branches, which is useful for rebasing, comparing branches, and understanding branch relationships.

Arguments

NameDescriptionOptional
commit Two or more commits to find the common ancestor of. No
--octopus Compute the best common ancestors of all supplied commits (for octopus merges). Yes
--is-ancestor Check if the first commit is an ancestor of the second commit. Yes
--fork-point Find the point at which a branch forked from another branch. Yes

Example

git merge-base main feature-branch
git merge-base --is-ancestor v1.0 main
git merge-base --fork-point main topic

Reference