git-merge-base
Find the best common ancestor for merging
TLDR
Print the best common ancestor of two commits
Print all best common ancestors of two commits
Check if a commit is an ancestor of a specific commit
SYNOPSIS
git merge-base <commit1> <commit2> [<commit3>...]
PARAMETERS
<commit1>
The first commit or branch name. Required.
<commit2>
The second commit or branch name. Required.
[<commit3>...]
Optional additional commits or branch names. The merge base common to all commits specified will be determined.
--all
Instead of printing only one merge base, output all merge bases. Useful when the branches have diverged in a complex manner.
--octopus
Find the merge base for a single n-way merge, instead of pairs of commits.
--fork-point
Instead of printing the merge base, output the point where the <commit> diverged from another branch (useful in topic branch workflows).
--independent
Output the list of commits given as input, that are not reachable from any other commit of the input. It is meant to be used as a building block to filter out commits supplied by the user.
--is-ancestor <commit1> <commit2>
Check if <commit1> is an ancestor of <commit2>. Exits with status 0 if true, 1 if false.
--onto <commit>
When used with --fork-point, consider only commits reachable from <commit>.
--no-repo
Do not try to use the repository. This can be useful if you are working in a non-Git directory.
DESCRIPTION
The git merge-base
command is a crucial tool for Git users to determine the best common ancestor(s) between two or more commits. This common ancestor, or merge base, is the commit from which both specified commits diverged. Identifying the merge base is fundamental in understanding the shared history between branches, making it invaluable for various Git operations such as conflict resolution, determining the range of changes between branches, and scripting custom Git workflows.
By default, git merge-base
outputs one or more commit IDs corresponding to the merge base(s). When multiple merge bases exist, it indicates that the branches have diverged in a complex manner (likely involving criss-cross merges). The command can be used with commit hashes, branch names (which resolve to the tip commit), or any other Git reference that resolves to a commit. Knowing the merge base helps developers understand how and when branches diverged, simplifying merging and rebasing operations.
CAVEATS
git merge-base
relies on the commit graph and can be computationally expensive for very large repositories or when comparing commits from deeply divergent branches. Performance can be improved by ensuring the repository's commit graph is well-maintained with regular git gc
operations.
EXIT STATUS
The command exits with 0 if successful, 1 if any error occurred, and 128 if any of the commits could not be found.
EXAMPLES
To find the merge base between the 'main' and 'feature' branches:git merge-base main feature
To check if commit 'A' is an ancestor of commit 'B':git merge-base --is-ancestor A B
HISTORY
git merge-base
has been a core part of Git since its inception. It evolved along with Git's branching model, providing a fundamental mechanism for understanding branch divergence. Its usage increased significantly as Git became the standard for distributed version control, with developers relying on it for resolving merge conflicts and automating branch management tasks.
SEE ALSO
git log(1), git merge(1), git rebase(1)