git-merge-base - Linux


Overview

git-merge-base finds a common base commit for two or more commits, trees, or working trees. It is used to determine the common ancestry between different versions of a project.

Syntax

git merge-base [options] <commit-ish> <commit-ish>

Options/Flags

  • -a: Compare all pairs of commits.
  • -c: Compare all commits in the current branch to the given commit.
  • -i: Find the most recent common ancestor of the given commits.
  • -n: Do not print the merge base, only return the number of commits on the shortest path.
  • -s: Print a summary of the merge base.

Examples

Find the common ancestry of two commits:

git merge-base commit1 commit2

Compare all pairs of commits:

git merge-base -a commit1 commit2 commit3

Find the most recent common ancestor of multiple commits:

git merge-base -i commit1 commit2 commit3

Print a summary of the merge base:

git merge-base -s commit1 commit2

Common Issues

  • Error: No merge base found.

This error occurs when there is no common ancestry between the given commits. Ensure that the commits are related and not independent branches.

  • Error: Too many merge bases found.

This error occurs when there are multiple common ancestors for the given commits. Use the -i option to find the most recent common ancestor.

Integration

git-merge-base can be used in conjunction with other Git commands, such as git merge, git cherry-pick, and git rebase, to manipulate the history and branching of a Git repository.

Related Commands