Appearance
Git Merge-Base — Quick Reference
Core commands
bash
# Find the best common ancestor
git merge-base main feature/branch
# All candidates (criss-cross merges)
git merge-base --all main feature/branch
# Check ancestor relationship (exit 0 = yes)
git merge-base --is-ancestor main HEAD
# Heuristic ancestor (survives upstream force-push)
git merge-base --fork-point origin/main HEAD
# Single ancestor across multiple branches
git merge-base --octopus main pr/42 pr/73
# Filter out refs that are ancestors of others in the list
git merge-base --independent release/2.3 release/2.4 mainCommon DevOps one-liners
bash
# Deploy gate: is COMMIT in main?
git merge-base --is-ancestor "$COMMIT" origin/main && echo "promote" || echo "block"
# Change-set against the actual divergence point
BASE=$(git merge-base origin/main HEAD)
git diff --name-only "$BASE" HEAD
# Merge check against explicit ancestor
BASE=$(git merge-base origin/main HEAD)
git merge-tree --write-tree --merge-base="$BASE" origin/main HEAD >/dev/null 2>&1
# Stale fork-point detection
git merge-base --fork-point origin/main HEAD || echo "branch needs fresh rebase"Key exit codes
| Code | Meaning |
|---|---|
| 0 | Ancestor found (or is-ancestor=true) |
| 1 | Not an ancestor (is-ancestor mode) or not found (fork-point) |
| 128 | Invalid ref or not a Git repository |
Config keys
| Key | Effect |
|---|---|
| (none) | No config keys control merge-base directly. Controlled entirely by arguments |
Pitfalls
- Fetch before resolving: stale
origin/maingives wrong base. - Fork-point needs the upstream reflog. Shallow clones lose it.
--is-ancestoris strict: A is not an ancestor of itself. UseA B || A == Bfor the inclusive check.--octopuscan return a non-ancestor in pathological criss-cross graphs. Validate with a loop.