Skip to content

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 main

Common 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 ​

CodeMeaning
0Ancestor found (or is-ancestor=true)
1Not an ancestor (is-ancestor mode) or not found (fork-point)
128Invalid ref or not a Git repository

Config keys ​

KeyEffect
(none)No config keys control merge-base directly. Controlled entirely by arguments

Pitfalls ​

  • Fetch before resolving: stale origin/main gives wrong base.
  • Fork-point needs the upstream reflog. Shallow clones lose it.
  • --is-ancestor is strict: A is not an ancestor of itself. Use A B || A == B for the inclusive check.
  • --octopus can return a non-ancestor in pathological criss-cross graphs. Validate with a loop.

Git Master — Git Reference for Every Skill Level