<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect2 SYSTEM "../../../dtd/dblite.dtd">

<sect2 lang="en" id="git-merge-base(1)">
    <title>git-merge-base(1)</title>
    <date>2024-07-02</date>
<revhistory><revision><date>2024-07-02</date></revision></revhistory>
<indexterm>
<primary>git-merge-base(1)</primary>
</indexterm>
<simplesect id="git-merge-base(1)__name">
<title>NAME</title>
<simpara>git-merge-base - Find as good common ancestors as possible for a merge</simpara>
</simplesect>
<simplesect id="git-merge-base(1)__synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git merge-base</emphasis> [-a | --all] &lt;commit&gt; &lt;commit&gt;&#8230;
<emphasis>git merge-base</emphasis> [-a | --all] --octopus &lt;commit&gt;&#8230;
<emphasis>git merge-base</emphasis> --is-ancestor &lt;commit&gt; &lt;commit&gt;
<emphasis>git merge-base</emphasis> --independent &lt;commit&gt;&#8230;
<emphasis>git merge-base</emphasis> --fork-point &lt;ref&gt; [&lt;commit&gt;]</literallayout>
</blockquote>
</simplesect>
<simplesect id="git-merge-base(1)__description">
<title>DESCRIPTION</title>
<simpara><emphasis>git merge-base</emphasis> finds the best common ancestor(s) between two commits to use
in a three-way merge.  One common ancestor is <emphasis>better</emphasis> than another common
ancestor if the latter is an ancestor of the former.  A common ancestor
that does not have any better common ancestor is a <emphasis>best common
ancestor</emphasis>, i.e. a <emphasis>merge base</emphasis>.  Note that there can be more than one
merge base for a pair of commits.</simpara>
</simplesect>
<simplesect id="git-merge-base(1)__operation_modes">
<title>OPERATION MODES</title>
<simpara>In the most common special case, specifying only two commits on the
command line means computing the merge base between the given two commits.</simpara>
<simpara>More generally, among the two commits to compute the merge base from,
one is specified by the first commit argument on the command line;
the other commit is a (possibly hypothetical) commit that is a merge
across all the remaining commits on the command line.</simpara>
<simpara>As a consequence, the <emphasis>merge base</emphasis> is not necessarily contained in each of the
commit arguments if more than two commits are specified. This is different
from <xref linkend="git-show-branch(1)" /> when used with the <emphasis>--merge-base</emphasis> option.</simpara>
<variablelist>
<varlistentry>
<term>
--octopus
</term>
<listitem>
<simpara>
        Compute the best common ancestors of all supplied commits,
        in preparation for an n-way merge.  This mimics the behavior
        of <emphasis>git show-branch --merge-base</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--independent
</term>
<listitem>
<simpara>
        Instead of printing merge bases, print a minimal subset of
        the supplied commits with the same ancestors.  In other words,
        among the commits given, list those which cannot be reached
        from any other.  This mimics the behavior of <emphasis>git show-branch
        --independent</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--is-ancestor
</term>
<listitem>
<simpara>
        Check if the first &lt;commit&gt; is an ancestor of the second &lt;commit&gt;,
        and exit with status 0 if true, or with status 1 if not.
        Errors are signaled by a non-zero status that is not 1.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--fork-point
</term>
<listitem>
<simpara>
        Find the point at which a branch (or any history that leads
        to &lt;commit&gt;) forked from another branch (or any reference)
        &lt;ref&gt;. This does not just look for the common ancestor of
        the two commits, but also takes into account the reflog of
        &lt;ref&gt; to see if the history leading to &lt;commit&gt; forked from
        an earlier incarnation of the branch &lt;ref&gt; (see discussion
        of this mode below).
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-merge-base(1)__options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
-a
</term>
<term>
--all
</term>
<listitem>
<simpara>
        Output all merge bases for the commits, instead of just one.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-merge-base(1)__discussion">
<title>DISCUSSION</title>
<simpara>Given two commits <emphasis>A</emphasis> and <emphasis>B</emphasis>, <emphasis>git merge-base A B</emphasis> will output a commit
which is reachable from both <emphasis>A</emphasis> and <emphasis>B</emphasis> through the parent relationship.</simpara>
<simpara>For example, with this topology:</simpara>
<literallayout class="monospaced">         o---o---o---B
        /
---o---1---o---o---o---A</literallayout>
<simpara>the merge base between <emphasis>A</emphasis> and <emphasis>B</emphasis> is <emphasis>1</emphasis>.</simpara>
<simpara>Given three commits <emphasis>A</emphasis>, <emphasis>B</emphasis>, and <emphasis>C</emphasis>, <emphasis>git merge-base A B C</emphasis> will compute the
merge base between <emphasis>A</emphasis> and a hypothetical commit <emphasis>M</emphasis>, which is a merge
between <emphasis>B</emphasis> and <emphasis>C</emphasis>.  For example, with this topology:</simpara>
<literallayout class="monospaced">       o---o---o---o---C
      /
     /   o---o---o---B
    /   /
---2---1---o---o---o---A</literallayout>
<simpara>the result of <emphasis>git merge-base A B C</emphasis> is <emphasis>1</emphasis>.  This is because the
equivalent topology with a merge commit <emphasis>M</emphasis> between <emphasis>B</emphasis> and <emphasis>C</emphasis> is:</simpara>
<literallayout class="monospaced">       o---o---o---o---o
      /                 \
     /   o---o---o---o---M
    /   /
---2---1---o---o---o---A</literallayout>
<simpara>and the result of <emphasis>git merge-base A M</emphasis> is <emphasis>1</emphasis>.  Commit <emphasis>2</emphasis> is also a
common ancestor between <emphasis>A</emphasis> and <emphasis>M</emphasis>, but <emphasis>1</emphasis> is a better common ancestor,
because <emphasis>2</emphasis> is an ancestor of <emphasis>1</emphasis>.  Hence, <emphasis>2</emphasis> is not a merge base.</simpara>
<simpara>The result of <emphasis>git merge-base --octopus A B C</emphasis> is <emphasis>2</emphasis>, because <emphasis>2</emphasis> is
the best common ancestor of all commits.</simpara>
<simpara>When the history involves criss-cross merges, there can be more than one
<emphasis>best</emphasis> common ancestor for two commits.  For example, with this topology:</simpara>
<literallayout class="monospaced">---1---o---A
    \ /
     X
    / \
---2---o---o---B</literallayout>
<simpara>both <emphasis>1</emphasis> and <emphasis>2</emphasis> are merge bases of A and B.  Neither one is better than
the other (both are <emphasis>best</emphasis> merge bases).  When the <emphasis>--all</emphasis> option is not given,
it is unspecified which best one is output.</simpara>
<simpara>A common idiom to check "fast-forward-ness" between two commits A
and B is (or at least used to be) to compute the merge base between
A and B, and check if it is the same as A, in which case, A is an
ancestor of B.  You will see this idiom used often in older scripts.</simpara>
<literallayout class="monospaced">A=$(git rev-parse --verify A)
if test "$A" = "$(git merge-base A B)"
then
        ... A is an ancestor of B ...
fi</literallayout>
<simpara>In modern git, you can say this in a more direct way:</simpara>
<literallayout class="monospaced">if git merge-base --is-ancestor A B
then
        ... A is an ancestor of B ...
fi</literallayout>
<simpara>instead.</simpara>
</simplesect>
<simplesect id="git-merge-base(1)__discussion_on_fork_point_mode">
<title>Discussion on fork-point mode</title>
<simpara>After working on the <emphasis>topic</emphasis> branch created with <emphasis>git switch -c
topic origin/master</emphasis>, the history of remote-tracking branch
<emphasis>origin/master</emphasis> may have been rewound and rebuilt, leading to a
history of this shape:</simpara>
<literallayout class="monospaced">                 o---B2
                /
---o---o---B1--o---o---o---B (origin/master)
        \
         B0
          \
           D0---D1---D (topic)</literallayout>
<simpara>where <emphasis>origin/master</emphasis> used to point at commits B0, B1, B2 and now it
points at B, and your <emphasis>topic</emphasis> branch was started on top of it back
when <emphasis>origin/master</emphasis> was at B0, and you built three commits, D0, D1,
and D, on top of it.  Imagine that you now want to rebase the work
you did on the topic on top of the updated origin/master.</simpara>
<simpara>In such a case, <emphasis>git merge-base origin/master topic</emphasis> would return the
parent of B0 in the above picture, but B0^..D is <emphasis role="strong">not</emphasis> the range of
commits you would want to replay on top of B (it includes B0, which
is not what you wrote; it is a commit the other side discarded when
it moved its tip from B0 to B1).</simpara>
<simpara><emphasis>git merge-base --fork-point origin/master topic</emphasis> is designed to
help in such a case.  It takes not only B but also B0, B1, and B2
(i.e. old tips of the remote-tracking branches your repository's
reflog knows about) into account to see on which commit your topic
branch was built and finds B0, allowing you to replay only the
commits on your topic, excluding the commits the other side later
discarded.</simpara>
<simpara>Hence</simpara>
<literallayout class="monospaced">$ fork_point=$(git merge-base --fork-point origin/master topic)</literallayout>
<simpara>will find B0, and</simpara>
<literallayout class="monospaced">$ git rebase --onto origin/master $fork_point topic</literallayout>
<simpara>will replay D0, D1, and D on top of B to create a new history of this
shape:</simpara>
<literallayout class="monospaced">                 o---B2
                /
---o---o---B1--o---o---o---B (origin/master)
        \                   \
         B0                  D0'--D1'--D' (topic - updated)
          \
           D0---D1---D (topic - old)</literallayout>
<simpara>A caveat is that older reflog entries in your repository may be
expired by <emphasis>git gc</emphasis>.  If B0 no longer appears in the reflog of the
remote-tracking branch <emphasis>origin/master</emphasis>, the <emphasis>--fork-point</emphasis> mode
obviously cannot find it and fails, avoiding to give a random and
useless result (such as the parent of B0, like the same command
without the <emphasis>--fork-point</emphasis> option gives).</simpara>
<simpara>Also, the remote-tracking branch you use the <emphasis>--fork-point</emphasis> mode
with must be the one your topic forked from its tip.  If you forked
from an older commit than the tip, this mode would not find the fork
point (imagine in the above sample history B0 did not exist,
origin/master started at B1, moved to B2 and then B, and you forked
your topic at origin/master^ when origin/master was B1; the shape of
the history would be the same as above, without B0, and the parent
of B1 is what <emphasis>git merge-base origin/master topic</emphasis> correctly finds,
but the <emphasis>--fork-point</emphasis> mode will not, because it is not one of the
commits that used to be at the tip of origin/master).</simpara>
</simplesect>
<simplesect id="git-merge-base(1)__see_also">
<title>See also</title>
<simpara><xref linkend="git-rev-list(1)" />,
<xref linkend="git-show-branch(1)" />,
<xref linkend="git-merge(1)" /></simpara>
</simplesect>
<simplesect id="git-merge-base(1)__git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)" /> suite</simpara>
</simplesect>
</sect2>
