Skip to content

Add merge-base #76

@mike-rambil

Description

@mike-rambil

Name

git merge-base

Category

Collaboration

Short Description

Find the common ancestor commit between two or more branches.

Long Description

git merge-base finds the best common ancestor(s) between two commits to use in a three-way merge. The common ancestor is the most recent commit that is reachable from both branches. This is useful for understanding where branches diverged, scripting merge operations, and debugging merge conflicts. Best commonly used to find common ancestor between a feature branch and main branch to squash commits locally.

Command

git merge-base <commit|branch> <commit|branch>

Examples

[
{
"code": "git merge-base main feature-branch",
"description": "Find the common ancestor between main and feature-branch."
},
{
"code": "git merge-base --all main feature-branch",
"description": "Find all common ancestors (useful when there are multiple merge bases)."
},
{
"code": "git merge-base --is-ancestor HEAD5 HEAD",
"description": "Check if HEAD
5 is an ancestor of HEAD (exits 0 if true, 1 if false)."
},
{
"code": "git merge-base --fork-point main",
"description": "Find the fork point where the current branch diverged from main."
}
]

Steps

[
"Identify the two branches or commits you want to compare.",
"Run git merge-base to find their common ancestor.",
"Use the resulting commit hash for further operations like diffing or rebasing."
]

Flags

{
"-a, --all": "Output all merge bases instead of just one.",
"--octopus": "Compute the best common ancestors of all supplied commits, for an n-way merge.",
"--independent": "Print a minimal subset of commits that cannot be reached from any other. Useful for filtering redundant commits.",
"--is-ancestor": "Check if the first commit is an ancestor of the second. Exits 0 if true, 1 if not.",
"--fork-point": "Find the point where a branch forked from a ref, using reflog history for accuracy."
}

Prerequisites

No response

Warnings

No response

ProTips

[
" Use it to find common branch between main and your feature branch to squash commits"]

Tags

[ "rebase", "PR", "merge-base", "merge"]

Author

mike-rambil

Last Updated

2025-01-20

Links

[
{
"label": "Official Docs",
"url": "https://git-scm.com/docs/git-merge-base"
}
]

Related Commands

[
"git merge",
"git rebase",
"git log",
"git diff"
]

Parent

Git Command Reference (Full List)

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions