Update git documentation
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_doc / git-merge-base.xml
blob6457fbac360b78208822503fea1e9f515419d41e
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE sect2 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">\r
3 \r
4 <sect2 lang="en" id="git-merge-base(1)">\r
5     <title>git-merge-base(1)</title>\r
6 <indexterm>\r
7 <primary>git-merge-base(1)</primary>\r
8 </indexterm>\r
9 <simplesect id="git-merge-base(1)__name">\r
10 <title>NAME</title>\r
11 <simpara>git-merge-base - Find as good common ancestors as possible for a merge</simpara>\r
12 </simplesect>\r
13 <simplesect id="git-merge-base(1)__synopsis">\r
14 <title>SYNOPSIS</title>\r
15 <blockquote>\r
16 <literallayout><emphasis>git merge-base</emphasis> [-a|--all] &lt;commit&gt; &lt;commit&gt;&#8230;\r
17 <emphasis>git merge-base</emphasis> [-a|--all] --octopus &lt;commit&gt;&#8230;\r
18 <emphasis>git merge-base</emphasis> --is-ancestor &lt;commit&gt; &lt;commit&gt;\r
19 <emphasis>git merge-base</emphasis> --independent &lt;commit&gt;&#8230;\r
20 <emphasis>git merge-base</emphasis> --fork-point &lt;ref&gt; [&lt;commit&gt;]</literallayout>\r
21 </blockquote>\r
22 </simplesect>\r
23 <simplesect id="git-merge-base(1)__description">\r
24 <title>DESCRIPTION</title>\r
25 <simpara><emphasis>git merge-base</emphasis> finds best common ancestor(s) between two commits to use\r
26 in a three-way merge.  One common ancestor is <emphasis>better</emphasis> than another common\r
27 ancestor if the latter is an ancestor of the former.  A common ancestor\r
28 that does not have any better common ancestor is a <emphasis>best common\r
29 ancestor</emphasis>, i.e. a <emphasis>merge base</emphasis>.  Note that there can be more than one\r
30 merge base for a pair of commits.</simpara>\r
31 </simplesect>\r
32 <simplesect id="git-merge-base(1)__operation_modes">\r
33 <title>OPERATION MODES</title>\r
34 <simpara>As the most common special case, specifying only two commits on the\r
35 command line means computing the merge base between the given two commits.</simpara>\r
36 <simpara>More generally, among the two commits to compute the merge base from,\r
37 one is specified by the first commit argument on the command line;\r
38 the other commit is a (possibly hypothetical) commit that is a merge\r
39 across all the remaining commits on the command line.</simpara>\r
40 <simpara>As a consequence, the <emphasis>merge base</emphasis> is not necessarily contained in each of the\r
41 commit arguments if more than two commits are specified. This is different\r
42 from <xref linkend="git-show-branch(1)" /> when used with the <emphasis>--merge-base</emphasis> option.</simpara>\r
43 <variablelist>\r
44 <varlistentry>\r
45 <term>\r
46 --octopus\r
47 </term>\r
48 <listitem>\r
49 <simpara>\r
50         Compute the best common ancestors of all supplied commits,\r
51         in preparation for an n-way merge.  This mimics the behavior\r
52         of <emphasis>git show-branch --merge-base</emphasis>.\r
53 </simpara>\r
54 </listitem>\r
55 </varlistentry>\r
56 <varlistentry>\r
57 <term>\r
58 --independent\r
59 </term>\r
60 <listitem>\r
61 <simpara>\r
62         Instead of printing merge bases, print a minimal subset of\r
63         the supplied commits with the same ancestors.  In other words,\r
64         among the commits given, list those which cannot be reached\r
65         from any other.  This mimics the behavior of <emphasis>git show-branch\r
66         --independent</emphasis>.\r
67 </simpara>\r
68 </listitem>\r
69 </varlistentry>\r
70 <varlistentry>\r
71 <term>\r
72 --is-ancestor\r
73 </term>\r
74 <listitem>\r
75 <simpara>\r
76         Check if the first &lt;commit&gt; is an ancestor of the second &lt;commit&gt;,\r
77         and exit with status 0 if true, or with status 1 if not.\r
78         Errors are signaled by a non-zero status that is not 1.\r
79 </simpara>\r
80 </listitem>\r
81 </varlistentry>\r
82 <varlistentry>\r
83 <term>\r
84 --fork-point\r
85 </term>\r
86 <listitem>\r
87 <simpara>\r
88         Find the point at which a branch (or any history that leads\r
89         to &lt;commit&gt;) forked from another branch (or any reference)\r
90         &lt;ref&gt;. This does not just look for the common ancestor of\r
91         the two commits, but also takes into account the reflog of\r
92         &lt;ref&gt; to see if the history leading to &lt;commit&gt; forked from\r
93         an earlier incarnation of the branch &lt;ref&gt; (see discussion\r
94         on this mode below).\r
95 </simpara>\r
96 </listitem>\r
97 </varlistentry>\r
98 </variablelist>\r
99 </simplesect>\r
100 <simplesect id="git-merge-base(1)__options">\r
101 <title>OPTIONS</title>\r
102 <variablelist>\r
103 <varlistentry>\r
104 <term>\r
105 -a\r
106 </term>\r
107 <term>\r
108 --all\r
109 </term>\r
110 <listitem>\r
111 <simpara>\r
112         Output all merge bases for the commits, instead of just one.\r
113 </simpara>\r
114 </listitem>\r
115 </varlistentry>\r
116 </variablelist>\r
117 </simplesect>\r
118 <simplesect id="git-merge-base(1)__discussion">\r
119 <title>DISCUSSION</title>\r
120 <simpara>Given two commits <emphasis>A</emphasis> and <emphasis>B</emphasis>, <emphasis>git merge-base A B</emphasis> will output a commit\r
121 which is reachable from both <emphasis>A</emphasis> and <emphasis>B</emphasis> through the parent relationship.</simpara>\r
122 <simpara>For example, with this topology:</simpara>\r
123 <literallayout class="monospaced">         o---o---o---B\r
124         /\r
125 ---o---1---o---o---o---A</literallayout>\r
126 <simpara>the merge base between <emphasis>A</emphasis> and <emphasis>B</emphasis> is <emphasis>1</emphasis>.</simpara>\r
127 <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\r
128 merge base between <emphasis>A</emphasis> and a hypothetical commit <emphasis>M</emphasis>, which is a merge\r
129 between <emphasis>B</emphasis> and <emphasis>C</emphasis>.  For example, with this topology:</simpara>\r
130 <literallayout class="monospaced">       o---o---o---o---C\r
131       /\r
132      /   o---o---o---B\r
133     /   /\r
134 ---2---1---o---o---o---A</literallayout>\r
135 <simpara>the result of <emphasis>git merge-base A B C</emphasis> is <emphasis>1</emphasis>.  This is because the\r
136 equivalent topology with a merge commit <emphasis>M</emphasis> between <emphasis>B</emphasis> and <emphasis>C</emphasis> is:</simpara>\r
137 <literallayout class="monospaced">       o---o---o---o---o\r
138       /                 \\r
139      /   o---o---o---o---M\r
140     /   /\r
141 ---2---1---o---o---o---A</literallayout>\r
142 <simpara>and the result of <emphasis>git merge-base A M</emphasis> is <emphasis>1</emphasis>.  Commit <emphasis>2</emphasis> is also a\r
143 common ancestor between <emphasis>A</emphasis> and <emphasis>M</emphasis>, but <emphasis>1</emphasis> is a better common ancestor,\r
144 because <emphasis>2</emphasis> is an ancestor of <emphasis>1</emphasis>.  Hence, <emphasis>2</emphasis> is not a merge base.</simpara>\r
145 <simpara>The result of <emphasis>git merge-base --octopus A B C</emphasis> is <emphasis>2</emphasis>, because <emphasis>2</emphasis> is\r
146 the best common ancestor of all commits.</simpara>\r
147 <simpara>When the history involves criss-cross merges, there can be more than one\r
148 <emphasis>best</emphasis> common ancestor for two commits.  For example, with this topology:</simpara>\r
149 <literallayout class="monospaced">---1---o---A\r
150     \ /\r
151      X\r
152     / \\r
153 ---2---o---o---B</literallayout>\r
154 <simpara>both <emphasis>1</emphasis> and <emphasis>2</emphasis> are merge-bases of A and B.  Neither one is better than\r
155 the other (both are <emphasis>best</emphasis> merge bases).  When the <emphasis>--all</emphasis> option is not given,\r
156 it is unspecified which best one is output.</simpara>\r
157 <simpara>A common idiom to check "fast-forward-ness" between two commits A\r
158 and B is (or at least used to be) to compute the merge base between\r
159 A and B, and check if it is the same as A, in which case, A is an\r
160 ancestor of B.  You will see this idiom used often in older scripts.</simpara>\r
161 <literallayout class="monospaced">A=$(git rev-parse --verify A)\r
162 if test "$A" = "$(git merge-base A B)"\r
163 then\r
164         ... A is an ancestor of B ...\r
165 fi</literallayout>\r
166 <simpara>In modern git, you can say this in a more direct way:</simpara>\r
167 <literallayout class="monospaced">if git merge-base --is-ancestor A B\r
168 then\r
169         ... A is an ancestor of B ...\r
170 fi</literallayout>\r
171 <simpara>instead.</simpara>\r
172 </simplesect>\r
173 <simplesect id="git-merge-base(1)__discussion_on_fork_point_mode">\r
174 <title>Discussion on fork-point mode</title>\r
175 <simpara>After working on the <emphasis>topic</emphasis> branch created with <emphasis>git checkout -b\r
176 topic origin/master</emphasis>, the history of remote-tracking branch\r
177 <emphasis>origin/master</emphasis> may have been rewound and rebuilt, leading to a\r
178 history of this shape:</simpara>\r
179 <literallayout class="monospaced">                 o---B1\r
180                 /\r
181 ---o---o---B2--o---o---o---B (origin/master)\r
182         \\r
183          B3\r
184           \\r
185            Derived (topic)</literallayout>\r
186 <simpara>where <emphasis>origin/master</emphasis> used to point at commits B3, B2, B1 and now it\r
187 points at B, and your <emphasis>topic</emphasis> branch was started on top of it back\r
188 when <emphasis>origin/master</emphasis> was at B3. This mode uses the reflog of\r
189 <emphasis>origin/master</emphasis> to find B3 as the fork point, so that the <emphasis>topic</emphasis>\r
190 can be rebased on top of the updated <emphasis>origin/master</emphasis> by:</simpara>\r
191 <literallayout class="monospaced">$ fork_point=$(git merge-base --fork-point origin/master topic)\r
192 $ git rebase --onto origin/master $fork_point topic</literallayout>\r
193 </simplesect>\r
194 <simplesect id="git-merge-base(1)__see_also">\r
195 <title>See also</title>\r
196 <simpara><xref linkend="git-rev-list(1)" />,\r
197 <xref linkend="git-show-branch(1)" />,\r
198 <xref linkend="git-merge(1)" /></simpara>\r
199 </simplesect>\r
200 <simplesect id="git-merge-base(1)__git">\r
201 <title>GIT</title>\r
202 <simpara>Part of the <xref linkend="git(1)" /> suite</simpara>\r
203 </simplesect>\r
204 </sect2>\r