<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-merge-base(1)">
<articleinfo>
    <title>git-merge-base(1)</title>
        <indexterm>
                <primary>git-merge-base(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-merge-base - Find as good common ancestors as possible for a merge</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<simpara><emphasis>git merge-base</emphasis> [--all] &lt;commit&gt; &lt;commit&gt;&#8230;</simpara>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara><emphasis>git-merge-base</emphasis> finds 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>
<simpara>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.  As the most common special case, specifying only
two commits on the command line means computing the merge base between
the given two commits.</simpara>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
--all
</term>
<listitem>
<simpara>
        Output all merge bases for the commits, instead of just one.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_discussion">
<title>DISCUSSION</title>
<simpara>Given two commits <emphasis>A</emphasis> and <emphasis>B</emphasis>, <literal>git merge-base A B</literal> 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>, <literal>git merge-base A B C</literal> 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 <literal>git merge-base A B C</literal> 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 <literal>git merge-base A M</literal> 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>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 <literal>--all</literal> option is not given,
it is unspecified which best one is output.</simpara>
</simplesect>
<simplesect id="_author">
<title>Author</title>
<simpara>Written by Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_documentation">
<title>Documentation</title>
<simpara>Documentation by David Greaves, Junio C Hamano and the git-list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;.</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
