Add Git official document to help
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_doc / git-read-tree.html.xml
blob3b7818a69066a37e11b9c1e074dd9affc3a36118
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">\r
3 \r
4 <article lang="en" id="git-read-tree(1)">\r
5 <articleinfo>\r
6     <title>git-read-tree(1)</title>\r
7         <indexterm>\r
8                 <primary>git-read-tree(1)</primary>\r
9         </indexterm>\r
10 </articleinfo>\r
11 <simplesect id="_name">\r
12 <title>NAME</title>\r
13 <simpara>git-read-tree - Reads tree information into the index</simpara>\r
14 </simplesect>\r
15 <simplesect id="_synopsis">\r
16 <title>SYNOPSIS</title>\r
17 <simpara><emphasis>git read-tree</emphasis> (&lt;tree-ish&gt; | [[-m [--trivial] [--aggressive] | --reset | --prefix=&lt;prefix&gt;] [-u | -i]] [--exclude-per-directory=&lt;gitignore&gt;] [--index-output=&lt;file&gt;] &lt;tree-ish1&gt; [&lt;tree-ish2&gt; [&lt;tree-ish3&gt;]])</simpara>\r
18 </simplesect>\r
19 <simplesect id="_description">\r
20 <title>DESCRIPTION</title>\r
21 <simpara>Reads the tree information given by &lt;tree-ish&gt; into the index,\r
22 but does not actually <emphasis role="strong">update</emphasis> any of the files it "caches". (see:\r
23 <xref linkend="git-checkout-index(1)"/>)</simpara>\r
24 <simpara>Optionally, it can merge a tree into the index, perform a\r
25 fast-forward (i.e. 2-way) merge, or a 3-way merge, with the <literal>-m</literal>\r
26 flag.  When used with <literal>-m</literal>, the <literal>-u</literal> flag causes it to also update\r
27 the files in the work tree with the result of the merge.</simpara>\r
28 <simpara>Trivial merges are done by <emphasis>git-read-tree</emphasis> itself.  Only conflicting paths\r
29 will be in unmerged state when <emphasis>git-read-tree</emphasis> returns.</simpara>\r
30 </simplesect>\r
31 <simplesect id="_options">\r
32 <title>OPTIONS</title>\r
33 <variablelist>\r
34 <varlistentry>\r
35 <term>\r
36 -m\r
37 </term>\r
38 <listitem>\r
39 <simpara>\r
40         Perform a merge, not just a read.  The command will\r
41         refuse to run if your index file has unmerged entries,\r
42         indicating that you have not finished previous merge you\r
43         started.\r
44 </simpara>\r
45 </listitem>\r
46 </varlistentry>\r
47 <varlistentry>\r
48 <term>\r
49 --reset\r
50 </term>\r
51 <listitem>\r
52 <simpara>\r
53         Same as -m, except that unmerged entries are discarded\r
54         instead of failing.\r
55 </simpara>\r
56 </listitem>\r
57 </varlistentry>\r
58 <varlistentry>\r
59 <term>\r
60 -u\r
61 </term>\r
62 <listitem>\r
63 <simpara>\r
64         After a successful merge, update the files in the work\r
65         tree with the result of the merge.\r
66 </simpara>\r
67 </listitem>\r
68 </varlistentry>\r
69 <varlistentry>\r
70 <term>\r
71 -i\r
72 </term>\r
73 <listitem>\r
74 <simpara>\r
75         Usually a merge requires the index file as well as the\r
76         files in the working tree are up to date with the\r
77         current head commit, in order not to lose local\r
78         changes.  This flag disables the check with the working\r
79         tree and is meant to be used when creating a merge of\r
80         trees that are not directly related to the current\r
81         working tree status into a temporary index file.\r
82 </simpara>\r
83 </listitem>\r
84 </varlistentry>\r
85 <varlistentry>\r
86 <term>\r
87 -v\r
88 </term>\r
89 <listitem>\r
90 <simpara>\r
91         Show the progress of checking files out.\r
92 </simpara>\r
93 </listitem>\r
94 </varlistentry>\r
95 <varlistentry>\r
96 <term>\r
97 --trivial\r
98 </term>\r
99 <listitem>\r
100 <simpara>\r
101         Restrict three-way merge by <emphasis>git-read-tree</emphasis> to happen\r
102         only if there is no file-level merging required, instead\r
103         of resolving merge for trivial cases and leaving\r
104         conflicting files unresolved in the index.\r
105 </simpara>\r
106 </listitem>\r
107 </varlistentry>\r
108 <varlistentry>\r
109 <term>\r
110 --aggressive\r
111 </term>\r
112 <listitem>\r
113 <simpara>\r
114         Usually a three-way merge by <emphasis>git-read-tree</emphasis> resolves\r
115         the merge for really trivial cases and leaves other\r
116         cases unresolved in the index, so that Porcelains can\r
117         implement different merge policies.  This flag makes the\r
118         command to resolve a few more cases internally:\r
119 </simpara>\r
120 <itemizedlist>\r
121 <listitem>\r
122 <simpara>\r
123 when one side removes a path and the other side leaves the path\r
124   unmodified.  The resolution is to remove that path.\r
125 </simpara>\r
126 </listitem>\r
127 <listitem>\r
128 <simpara>\r
129 when both sides remove a path.  The resolution is to remove that path.\r
130 </simpara>\r
131 </listitem>\r
132 <listitem>\r
133 <simpara>\r
134 when both sides adds a path identically.  The resolution\r
135   is to add that path.\r
136 </simpara>\r
137 </listitem>\r
138 </itemizedlist>\r
139 </listitem>\r
140 </varlistentry>\r
141 <varlistentry>\r
142 <term>\r
143 --prefix=&lt;prefix&gt;/\r
144 </term>\r
145 <listitem>\r
146 <simpara>\r
147         Keep the current index contents, and read the contents\r
148         of named tree-ish under directory at <literal>&lt;prefix&gt;</literal>.  The\r
149         original index file cannot have anything at the path\r
150         <literal>&lt;prefix&gt;</literal> itself, and have nothing in <literal>&lt;prefix&gt;/</literal>\r
151         directory.  Note that the <literal>&lt;prefix&gt;/</literal> value must end\r
152         with a slash.\r
153 </simpara>\r
154 </listitem>\r
155 </varlistentry>\r
156 <varlistentry>\r
157 <term>\r
158 --exclude-per-directory=&lt;gitignore&gt;\r
159 </term>\r
160 <listitem>\r
161 <simpara>\r
162         When running the command with <literal>-u</literal> and <literal>-m</literal> options, the\r
163         merge result may need to overwrite paths that are not\r
164         tracked in the current branch.  The command usually\r
165         refuses to proceed with the merge to avoid losing such a\r
166         path.  However this safety valve sometimes gets in the\r
167         way.  For example, it often happens that the other\r
168         branch added a file that used to be a generated file in\r
169         your branch, and the safety valve triggers when you try\r
170         to switch to that branch after you ran <literal>make</literal> but before\r
171         running <literal>make clean</literal> to remove the generated file.  This\r
172         option tells the command to read per-directory exclude\r
173         file (usually <emphasis>.gitignore</emphasis>) and allows such an untracked\r
174         but explicitly ignored file to be overwritten.\r
175 </simpara>\r
176 </listitem>\r
177 </varlistentry>\r
178 <varlistentry>\r
179 <term>\r
180 --index-output=&lt;file&gt;\r
181 </term>\r
182 <listitem>\r
183 <simpara>\r
184         Instead of writing the results out to <literal>$GIT_INDEX_FILE</literal>,\r
185         write the resulting index in the named file.  While the\r
186         command is operating, the original index file is locked\r
187         with the same mechanism as usual.  The file must allow\r
188         to be rename(2)ed into from a temporary file that is\r
189         created next to the usual index file; typically this\r
190         means it needs to be on the same filesystem as the index\r
191         file itself, and you need write permission to the\r
192         directories the index file and index output file are\r
193         located in.\r
194 </simpara>\r
195 </listitem>\r
196 </varlistentry>\r
197 <varlistentry>\r
198 <term>\r
199 &lt;tree-ish#&gt;\r
200 </term>\r
201 <listitem>\r
202 <simpara>\r
203         The id of the tree object(s) to be read/merged.\r
204 </simpara>\r
205 </listitem>\r
206 </varlistentry>\r
207 </variablelist>\r
208 </simplesect>\r
209 <simplesect id="_merging">\r
210 <title>Merging</title>\r
211 <simpara>If <literal>-m</literal> is specified, <emphasis>git-read-tree</emphasis> can perform 3 kinds of\r
212 merge, a single tree merge if only 1 tree is given, a\r
213 fast-forward merge with 2 trees, or a 3-way merge if 3 trees are\r
214 provided.</simpara>\r
215 <simplesect id="_single_tree_merge">\r
216 <title>Single Tree Merge</title>\r
217 <simpara>If only 1 tree is specified, <emphasis>git-read-tree</emphasis> operates as if the user did not\r
218 specify <literal>-m</literal>, except that if the original index has an entry for a\r
219 given pathname, and the contents of the path matches with the tree\r
220 being read, the stat info from the index is used. (In other words, the\r
221 index&#8217;s stat()s take precedence over the merged tree&#8217;s).</simpara>\r
222 <simpara>That means that if you do a <literal>git read-tree -m &lt;newtree&gt;</literal> followed by a\r
223 <literal>git checkout-index -f -u -a</literal>, the <emphasis>git-checkout-index</emphasis> only checks out\r
224 the stuff that really changed.</simpara>\r
225 <simpara>This is used to avoid unnecessary false hits when <emphasis>git-diff-files</emphasis> is\r
226 run after <emphasis>git-read-tree</emphasis>.</simpara>\r
227 </simplesect>\r
228 <simplesect id="_two_tree_merge">\r
229 <title>Two Tree Merge</title>\r
230 <simpara>Typically, this is invoked as <literal>git read-tree -m $H $M</literal>, where $H\r
231 is the head commit of the current repository, and $M is the head\r
232 of a foreign tree, which is simply ahead of $H (i.e. we are in a\r
233 fast forward situation).</simpara>\r
234 <simpara>When two trees are specified, the user is telling <emphasis>git-read-tree</emphasis>\r
235 the following:</simpara>\r
236 <orderedlist numeration="arabic">\r
237 <listitem>\r
238 <simpara>\r
239 The current index and work tree is derived from $H, but\r
240         the user may have local changes in them since $H;\r
241 </simpara>\r
242 </listitem>\r
243 <listitem>\r
244 <simpara>\r
245 The user wants to fast-forward to $M.\r
246 </simpara>\r
247 </listitem>\r
248 </orderedlist>\r
249 <simpara>In this case, the <literal>git read-tree -m $H $M</literal> command makes sure\r
250 that no local change is lost as the result of this "merge".\r
251 Here are the "carry forward" rules:</simpara>\r
252 <literallayout class="monospaced">  I (index)           H        M        Result\r
253  -------------------------------------------------------\r
254 0 nothing             nothing  nothing  (does not happen)\r
255 1 nothing             nothing  exists   use M\r
256 2 nothing             exists   nothing  remove path from index\r
257 3 nothing             exists   exists,  use M if "initial checkout"\r
258                                H == M   keep index otherwise\r
259                                exists   fail\r
260                                H != M</literallayout>\r
261 <literallayout class="monospaced">  clean I==H  I==M\r
262  ------------------\r
263 4 yes   N/A   N/A     nothing  nothing  keep index\r
264 5 no    N/A   N/A     nothing  nothing  keep index</literallayout>\r
265 <literallayout class="monospaced">6 yes   N/A   yes     nothing  exists   keep index\r
266 7 no    N/A   yes     nothing  exists   keep index\r
267 8 yes   N/A   no      nothing  exists   fail\r
268 9 no    N/A   no      nothing  exists   fail</literallayout>\r
269 <literallayout class="monospaced">10 yes   yes   N/A     exists   nothing  remove path from index\r
270 11 no    yes   N/A     exists   nothing  fail\r
271 12 yes   no    N/A     exists   nothing  fail\r
272 13 no    no    N/A     exists   nothing  fail</literallayout>\r
273 <literallayout class="monospaced">   clean (H=M)\r
274   ------\r
275 14 yes                 exists   exists   keep index\r
276 15 no                  exists   exists   keep index</literallayout>\r
277 <literallayout class="monospaced">   clean I==H  I==M (H!=M)\r
278   ------------------\r
279 16 yes   no    no      exists   exists   fail\r
280 17 no    no    no      exists   exists   fail\r
281 18 yes   no    yes     exists   exists   keep index\r
282 19 no    no    yes     exists   exists   keep index\r
283 20 yes   yes   no      exists   exists   use M\r
284 21 no    yes   no      exists   exists   fail</literallayout>\r
285 <simpara>In all "keep index" cases, the index entry stays as in the\r
286 original index file.  If the entry were not up to date,\r
287 <emphasis>git-read-tree</emphasis> keeps the copy in the work tree intact when\r
288 operating under the -u flag.</simpara>\r
289 <simpara>When this form of <emphasis>git-read-tree</emphasis> returns successfully, you can\r
290 see what "local changes" you made are carried forward by running\r
291 <literal>git diff-index --cached $M</literal>.  Note that this does not\r
292 necessarily match <literal>git diff-index --cached $H</literal> would have\r
293 produced before such a two tree merge.  This is because of cases\r
294 18 and 19 --- if you already had the changes in $M (e.g. maybe\r
295 you picked it up via e-mail in a patch form), <literal>git diff-index\r
296 --cached $H</literal> would have told you about the change before this\r
297 merge, but it would not show in <literal>git diff-index --cached $M</literal>\r
298 output after two-tree merge.</simpara>\r
299 <simpara>Case #3 is slightly tricky and needs explanation.  The result from this\r
300 rule logically should be to remove the path if the user staged the removal\r
301 of the path and then switching to a new branch.  That however will prevent\r
302 the initial checkout from happening, so the rule is modified to use M (new\r
303 tree) only when the contents of the index is empty.  Otherwise the removal\r
304 of the path is kept as long as $H and $M are the same.</simpara>\r
305 </simplesect>\r
306 <simplesect id="_3_way_merge">\r
307 <title>3-Way Merge</title>\r
308 <simpara>Each "index" entry has two bits worth of "stage" state. stage 0 is the\r
309 normal one, and is the only one you&#8217;d see in any kind of normal use.</simpara>\r
310 <simpara>However, when you do <emphasis>git-read-tree</emphasis> with three trees, the "stage"\r
311 starts out at 1.</simpara>\r
312 <simpara>This means that you can do</simpara>\r
313 <literallayout>$ git read-tree -m &lt;tree1&gt; &lt;tree2&gt; &lt;tree3&gt;</literallayout>\r
314 <simpara>and you will end up with an index with all of the &lt;tree1&gt; entries in\r
315 "stage1", all of the &lt;tree2&gt; entries in "stage2" and all of the\r
316 &lt;tree3&gt; entries in "stage3".  When performing a merge of another\r
317 branch into the current branch, we use the common ancestor tree\r
318 as &lt;tree1&gt;, the current branch head as &lt;tree2&gt;, and the other\r
319 branch head as &lt;tree3&gt;.</simpara>\r
320 <simpara>Furthermore, <emphasis>git-read-tree</emphasis> has special-case logic that says: if you see\r
321 a file that matches in all respects in the following states, it\r
322 "collapses" back to "stage0":</simpara>\r
323 <itemizedlist>\r
324 <listitem>\r
325 <simpara>\r
326 stage 2 and 3 are the same; take one or the other (it makes no\r
327      difference - the same work has been done on our branch in\r
328      stage 2 and their branch in stage 3)\r
329 </simpara>\r
330 </listitem>\r
331 <listitem>\r
332 <simpara>\r
333 stage 1 and stage 2 are the same and stage 3 is different; take\r
334      stage 3 (our branch in stage 2 did not do anything since the\r
335      ancestor in stage 1 while their branch in stage 3 worked on\r
336      it)\r
337 </simpara>\r
338 </listitem>\r
339 <listitem>\r
340 <simpara>\r
341 stage 1 and stage 3 are the same and stage 2 is different take\r
342      stage 2 (we did something while they did nothing)\r
343 </simpara>\r
344 </listitem>\r
345 </itemizedlist>\r
346 <simpara>The <emphasis>git-write-tree</emphasis> command refuses to write a nonsensical tree, and it\r
347 will complain about unmerged entries if it sees a single entry that is not\r
348 stage 0.</simpara>\r
349 <simpara>OK, this all sounds like a collection of totally nonsensical rules,\r
350 but it&#8217;s actually exactly what you want in order to do a fast\r
351 merge. The different stages represent the "result tree" (stage 0, aka\r
352 "merged"), the original tree (stage 1, aka "orig"), and the two trees\r
353 you are trying to merge (stage 2 and 3 respectively).</simpara>\r
354 <simpara>The order of stages 1, 2 and 3 (hence the order of three\r
355 &lt;tree-ish&gt; command line arguments) are significant when you\r
356 start a 3-way merge with an index file that is already\r
357 populated.  Here is an outline of how the algorithm works:</simpara>\r
358 <itemizedlist>\r
359 <listitem>\r
360 <simpara>\r
361 if a file exists in identical format in all three trees, it will\r
362   automatically collapse to "merged" state by <emphasis>git-read-tree</emphasis>.\r
363 </simpara>\r
364 </listitem>\r
365 <listitem>\r
366 <simpara>\r
367 a file that has <emphasis>any</emphasis> difference what-so-ever in the three trees\r
368   will stay as separate entries in the index. It&#8217;s up to "porcelain\r
369   policy" to determine how to remove the non-0 stages, and insert a\r
370   merged version.\r
371 </simpara>\r
372 </listitem>\r
373 <listitem>\r
374 <simpara>\r
375 the index file saves and restores with all this information, so you\r
376   can merge things incrementally, but as long as it has entries in\r
377   stages 1/2/3 (i.e., "unmerged entries") you can&#8217;t write the result. So\r
378   now the merge algorithm ends up being really simple:\r
379 </simpara>\r
380 <itemizedlist>\r
381 <listitem>\r
382 <simpara>\r
383 you walk the index in order, and ignore all entries of stage 0,\r
384     since they&#8217;ve already been done.\r
385 </simpara>\r
386 </listitem>\r
387 <listitem>\r
388 <simpara>\r
389 if you find a "stage1", but no matching "stage2" or "stage3", you\r
390     know it&#8217;s been removed from both trees (it only existed in the\r
391     original tree), and you remove that entry.\r
392 </simpara>\r
393 </listitem>\r
394 <listitem>\r
395 <simpara>\r
396 if you find a matching "stage2" and "stage3" tree, you remove one\r
397     of them, and turn the other into a "stage0" entry. Remove any\r
398     matching "stage1" entry if it exists too.  .. all the normal\r
399     trivial rules ..\r
400 </simpara>\r
401 </listitem>\r
402 </itemizedlist>\r
403 </listitem>\r
404 </itemizedlist>\r
405 <simpara>You would normally use <emphasis>git-merge-index</emphasis> with supplied\r
406 <emphasis>git-merge-one-file</emphasis> to do this last step.  The script updates\r
407 the files in the working tree as it merges each path and at the\r
408 end of a successful merge.</simpara>\r
409 <simpara>When you start a 3-way merge with an index file that is already\r
410 populated, it is assumed that it represents the state of the\r
411 files in your work tree, and you can even have files with\r
412 changes unrecorded in the index file.  It is further assumed\r
413 that this state is "derived" from the stage 2 tree.  The 3-way\r
414 merge refuses to run if it finds an entry in the original index\r
415 file that does not match stage 2.</simpara>\r
416 <simpara>This is done to prevent you from losing your work-in-progress\r
417 changes, and mixing your random changes in an unrelated merge\r
418 commit.  To illustrate, suppose you start from what has been\r
419 committed last to your repository:</simpara>\r
420 <literallayout>$ JC=`git rev-parse --verify "HEAD^0"`\r
421 $ git checkout-index -f -u -a $JC</literallayout>\r
422 <simpara>You do random edits, without running <emphasis>git-update-index</emphasis>.  And then\r
423 you notice that the tip of your "upstream" tree has advanced\r
424 since you pulled from him:</simpara>\r
425 <literallayout>$ git fetch git://.... linus\r
426 $ LT=`cat .git/FETCH_HEAD`</literallayout>\r
427 <simpara>Your work tree is still based on your HEAD ($JC), but you have\r
428 some edits since.  Three-way merge makes sure that you have not\r
429 added or modified index entries since $JC, and if you haven&#8217;t,\r
430 then does the right thing.  So with the following sequence:</simpara>\r
431 <literallayout>$ git read-tree -m -u `git merge-base $JC $LT` $JC $LT\r
432 $ git merge-index git-merge-one-file -a\r
433 $ echo "Merge with Linus" | \\r
434   git commit-tree `git write-tree` -p $JC -p $LT</literallayout>\r
435 <simpara>what you would commit is a pure merge between $JC and $LT without\r
436 your work-in-progress changes, and your work tree would be\r
437 updated to the result of the merge.</simpara>\r
438 <simpara>However, if you have local changes in the working tree that\r
439 would be overwritten by this merge, <emphasis>git-read-tree</emphasis> will refuse\r
440 to run to prevent your changes from being lost.</simpara>\r
441 <simpara>In other words, there is no need to worry about what exists only\r
442 in the working tree.  When you have local changes in a part of\r
443 the project that is not involved in the merge, your changes do\r
444 not interfere with the merge, and are kept intact.  When they\r
445 <emphasis role="strong">do</emphasis> interfere, the merge does not even start (<emphasis>git-read-tree</emphasis>\r
446 complains loudly and fails without modifying anything).  In such\r
447 a case, you can simply continue doing what you were in the\r
448 middle of doing, and when your working tree is ready (i.e. you\r
449 have finished your work-in-progress), attempt the merge again.</simpara>\r
450 </simplesect>\r
451 </simplesect>\r
452 <simplesect id="_see_also">\r
453 <title>SEE ALSO</title>\r
454 <simpara><xref linkend="git-write-tree(1)"/>; <xref linkend="git-ls-files(1)"/>;\r
455 <xref linkend="gitignore(5)"/></simpara>\r
456 </simplesect>\r
457 <simplesect id="_author">\r
458 <title>Author</title>\r
459 <simpara>Written by Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>\r
460 </simplesect>\r
461 <simplesect id="_documentation">\r
462 <title>Documentation</title>\r
463 <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>\r
464 </simplesect>\r
465 <simplesect id="_git">\r
466 <title>GIT</title>\r
467 <simpara>Part of the <xref linkend="git(1)"/> suite</simpara>\r
468 </simplesect>\r
469 </article>\r