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 featureOptions
| Flag | What it does |
|---|---|
| --is-ancestor | Exit 0 if first commit is an ancestor of second |
| --fork-point | Use reflog to find where a branch forked |
| --all | Print all merge bases (for criss-cross merges) |
| --octopus | Best 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
git diff: Usage, Options & Common CI Errorsgit diff shows changes between commits, the index, and the working tree. Reference for --staged, --name-only,…
git log: Usage, Options & Common CI Errorsgit log shows commit history with flexible formatting. Reference for --oneline, --graph, --pretty, ranges, an…
git rev-parse: Usage, Options & Common CI Errorsgit rev-parse resolves refs to SHAs and reports repo paths - core CI plumbing. Reference for --short, --abbre…
git fetch: Usage, Options & Common CI Errorsgit fetch downloads remote objects and refs without touching your working tree. Reference for --depth, --unsh…