LinuxCommandLibrary

git-merge-base

Find the best common ancestor for merging

TLDR

Print the best common ancestor of two commits

$ git merge-base [commit_1] [commit_2]
copy

Print all best common ancestors of two commits
$ git merge-base [[-a|--all]] [commit_1] [commit_2]
copy

Check if a commit is an ancestor of a specific commit
$ git merge-base --is-ancestor [ancestor_commit] [commit]
copy

SYNOPSIS

git merge-base [options] <commit1> <commit2> [<commit3> ...]
git merge-base --is-ancestor <commit1> <commit2>
git merge-base --fork-point <ref> [--upstream <upstream>] [<commit>]

PARAMETERS

--all
    Output all common ancestors, not just the best one(s).

--octopus
    Compute common ancestors for >2 commits using octopus merge strategy (default).

--independent
    Output only ancestors not reachable from other candidates.

--is-ancestor <commit1> <commit2>
    Exit 0 if commit1 is ancestor of commit2, 1 otherwise.

--fork-point <ref> [--upstream <upstream>] [<commit>]
    Find last commit on ref before divergence from current branch (<commit> defaults to HEAD).

--upstream <upstream>
    Specify upstream ref for --fork-point (alternative to default).

-h, --help
    Print usage and exit.

-v, --verbose
    Synonym for --all (legacy).

DESCRIPTION

git merge-base is a Git plumbing command that identifies the best common ancestor(s), known as the merge base, between two or more commits. The merge base serves as the divergence point in the commit graph, essential for three-way merges.

In simple cases with two commits, it outputs the SHA-1 of the lowest common ancestor. For branched histories, it selects the "best" base minimizing conflicting changes. With --all, it lists all common ancestors.

Used internally by git merge, git rebase, and git cherry-pick. Supports multi-commit modes via --octopus (default for >2 commits) or --independent. Ancestry checks via --is-ancestor return exit codes: 0 (yes), 1 (no). Fork-point mode aids topic branch rebasing against upstream.

Key in Git workflows for safe merging, avoiding unnecessary conflicts by basing diffs on the true split point.

CAVEATS

Multiple merge bases possible in criss-cross merges; default picks one via diffstat heuristic.
--all recommended for safety. Commits/refs resolved via git rev-parse; invalid refs error out. Not for merge commits directly.

EXAMPLES

git merge-base main feature → SHA of divergence.
git merge-base --all main feature → All common ancestors.
git merge-base --fork-point origin/main → Rebase target for topic branch.
git merge-base --is-ancestor HEAD~10 HEAD → Check ancestry.

EXIT STATUS

0: success/best base found or is-ancestor true.
1: no common ancestor or is-ancestor false.
128: usage error/invalid refs.

HISTORY

Introduced in Git 1.0.0 (2005) for basic two-commit merge bases. Added --all, --octopus in 1.5.x for multi-merge. --independent (1.6.3, 2008), --fork-point (1.7.4, 2010) for rebase workflows. Ongoing refinements in Git 2.x for edge cases.

SEE ALSO

Copied to clipboard