Git Reference ยท Merging and Rebasing
git merge-base command
Reference for git merge-base: find the best common ancestor of commits, test whether one commit is an ancestor of another and use the fork point of a branch.
git merge-base prints the best common ancestor of two or more commits: the newest commit that all of them can reach. It is the starting point every three-way merge uses, and it is also the quickest way to answer questions such as "where did this branch split off?" or "is this branch already merged?".
Two modes cover most uses. The default prints the base. --is-ancestor prints nothing and reports through the exit status, so it fits directly into an if in a shell script.
git merge-base main feature # the fork point git merge-base --is-ancestor A B # is A reachable from B? git diff $(git merge-base main feature) feature # what the feature added
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git merge-base [-a | --all] <commit> <commit>... git merge-base [-a | --all] --octopus <commit>... git merge-base --is-ancestor <commit> <commit> git merge-base --independent <commit>... git merge-base --fork-point <ref> [<commit>]
| Name | Description |
|---|---|
<commit> |
Two or more revisions. With more than two, the base of the first against a merge of the rest is reported (see --octopus). |
-a, --all |
Print every best common ancestor, not just one. |
--octopus |
Find the best common ancestors of all the given commits at once, as an octopus merge would. |
--is-ancestor |
Print nothing. Exit 0 if the first commit is an ancestor of the second, 1 if not. |
--independent |
From the list, print only commits that are not reachable from any other listed commit. |
--fork-point |
Find where <commit> (default HEAD) forked from the reflog history of <ref>. |
Output: a full commit id. Exit status: 0 when a base is found, 1 when there is none (unrelated histories), 128 on invalid input.
Usage notes
- Ancestor means "itself or a parent chain". A commit is its own ancestor, so
--is-ancestor X Xsucceeds. This is why a branch that was merged (or is identical) reports success. - Criss-cross merges produce several equally good bases. Plain
merge-basepicks one;--allshows them. Theortstrategy merges the bases first to make a virtual base, see Merge strategies. - Three dots mean the merge base.
git diff A...BcomparesBwithmerge-base(A, B), andgit log A...Bis the symmetric difference. You rarely need to callmerge-basefor those. --fork-pointfights rebased upstreams. If the upstream branch was rewritten, the plain merge base can be too old.--fork-pointconsults the upstream's reflog to find where your branch really started;git rebaseuses it by default when it has an upstream. The reflog is local, so it fails in a fresh clone.- Merged-into checks.
git merge-base --is-ancestor feature mainis the exact test behindgit branch --merged. - Cost. On very large histories Git uses commit-graph generation numbers to answer quickly. Writing the commit-graph is described in
git maintenance. - Shallow clones may lack the real base. Deepen the clone with
git fetch --deepenfirst.