Skip to content
Latchkey

git merge-base: Usage, Options & Common CI Errors

git merge-base returns the best common ancestor of two commits - the point your branch diverged.

merge-base is how CI computes "changed in this PR": diff against the merge base of the branch and its target.

What it does

git merge-base finds the commit where two branches diverged (their best common ancestor), which is the correct base for computing a pull request’s effective diff.

Common usage

Terminal
git merge-base main feature
git diff $(git merge-base main HEAD) HEAD   # PR changes only
git merge-base --is-ancestor A B && echo "A is ancestor of B"
git merge-base --fork-point main feature

Options

FlagWhat it does
--is-ancestorExit 0 if first commit is an ancestor of second
--fork-pointUse reflog to find where a branch forked
--allPrint all merge bases (for criss-cross merges)
--octopusBest common ancestor of many commits

Common errors in CI

fatal: Not a valid commit name or an empty result usually means the base branch is missing on the runner - a shallow or single-branch clone. Fetch the target branch (git fetch origin main) or set fetch-depth: 0 so the common ancestor is reachable.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →