git-read-tree(1) (original) (raw)

If -m is specified, git read-tree can perform 3 kinds of merge, a single tree merge if only 1 tree is given, a fast-forward merge with 2 trees, or a 3-way merge if 3 or more trees are provided.

Single Tree Merge

If only 1 tree is specified, git read-tree operates as if the user did not specify -m, except that if the original index has an entry for a given pathname, and the contents of the path match with the tree being read, the stat info from the index is used. (In other words, the index’s stat()s take precedence over the merged tree’s).

That means that if you do a git read-tree -m followed by agit checkout-index -f -u -a, the git checkout-index only checks out the stuff that really changed.

This is used to avoid unnecessary false hits when git diff-files is run after git read-tree.

Two Tree Merge

Typically, this is invoked as git read-tree -m $H $M, where Histheheadcommitofthecurrentrepository,andH is the head commit of the current repository, and Histheheadcommitofthecurrentrepository,andM is the head of a foreign tree, which is simply ahead of $H (i.e. we are in a fast-forward situation).

When two trees are specified, the user is telling _git read-tree_the following:

  1. The current index and work tree is derived from H,buttheusermayhavelocalchangesinthemsinceH, but the user may have local changes in them since H,buttheusermayhavelocalchangesinthemsinceH.
  2. The user wants to fast-forward to $M.

In this case, the git read-tree -m $H $M command makes sure that no local change is lost as the result of this "merge". Here are the "carry forward" rules, where "I" denotes the index, "clean" means that index and work tree coincide, and "exists"/"nothing" refer to the presence of a path in the specified commit:

    I                   H        M        Result
   -------------------------------------------------------
 0  nothing             nothing  nothing  (does not happen)
 1  nothing             nothing  exists   use M
 2  nothing             exists   nothing  remove path from index
 3  nothing             exists   exists,  use M if "initial checkout",
                                 H == M   keep index otherwise
                                 exists,  fail
                                 H != M

    clean I==H  I==M
   ------------------
 4  yes   N/A   N/A     nothing  nothing  keep index
 5  no    N/A   N/A     nothing  nothing  keep index

 6  yes   N/A   yes     nothing  exists   keep index
 7  no    N/A   yes     nothing  exists   keep index
 8  yes   N/A   no      nothing  exists   fail
 9  no    N/A   no      nothing  exists   fail

 10 yes   yes   N/A     exists   nothing  remove path from index
 11 no    yes   N/A     exists   nothing  fail
 12 yes   no    N/A     exists   nothing  fail
 13 no    no    N/A     exists   nothing  fail

    clean (H==M)
   ------
 14 yes                 exists   exists   keep index
 15 no                  exists   exists   keep index

    clean I==H  I==M (H!=M)
   ------------------
 16 yes   no    no      exists   exists   fail
 17 no    no    no      exists   exists   fail
 18 yes   no    yes     exists   exists   keep index
 19 no    no    yes     exists   exists   keep index
 20 yes   yes   no      exists   exists   use M
 21 no    yes   no      exists   exists   fail

In all "keep index" cases, the index entry stays as in the original index file. If the entry is not up to date,git read-tree keeps the copy in the work tree intact when operating under the -u flag.

When this form of git read-tree returns successfully, you can see which of the "local changes" that you made were carried forward by runninggit diff-index --cached $M. Note that this does not necessarily match what git diff-index --cached $H would have produced before such a two tree merge. This is because of cases 18 and 19 — if you already had the changes in M(e.g.maybeyoupickeditupviae−mailinapatchform),‘git‘‘diff−index‘‘−−cached‘‘M (e.g. maybe you picked it up via e-mail in a patch form), git diff-index --cached </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mopen">(</span><span class="mord mathnormal">e</span><span class="mord">.</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord">.</span><span class="mord mathnormal">ma</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal">b</span><span class="mord mathnormal">eyo</span><span class="mord mathnormal">u</span><span class="mord mathnormal">p</span><span class="mord mathnormal">i</span><span class="mord mathnormal">c</span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord mathnormal">i</span><span class="mord mathnormal">t</span><span class="mord mathnormal">u</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">ia</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">mai</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ina</span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">c</span><span class="mord mathnormal">h</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">m</span><span class="mclose">)</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">‘</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">i</span><span class="mord mathnormal">t</span><span class="mord">‘‘</span><span class="mord mathnormal">d</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.10764em;">ff</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord">‘‘</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord">−</span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord mathnormal">c</span><span class="mord mathnormal">h</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord">‘‘</span></span></span></span>H would have told you about the change before this merge, but it would not show in git diff-index --cached $Moutput after the two-tree merge.

Case 3 is slightly tricky and needs explanation. The result from this rule logically should be to remove the path if the user staged the removal of the path and then switching to a new branch. That however will prevent the initial checkout from happening, so the rule is modified to use M (new tree) only when the content of the index is empty. Otherwise the removal of the path is kept as long as HandH and HandM are the same.

3-Way Merge

Each "index" entry has two bits worth of "stage" state. stage 0 is the normal one, and is the only one you’d see in any kind of normal use.

However, when you do git read-tree with three trees, the "stage" starts out at 1.

This means that you can do

$ git read-tree -m

and you will end up with an index with all of the entries in "stage1", all of the entries in "stage2" and all of the entries in "stage3". When performing a merge of another branch into the current branch, we use the common ancestor tree as , the current branch head as , and the other branch head as .

Furthermore, git read-tree has special-case logic that says: if you see a file that matches in all respects in the following states, it "collapses" back to "stage0":

The git write-tree command refuses to write a nonsensical tree, and it will complain about unmerged entries if it sees a single entry that is not stage 0.

OK, this all sounds like a collection of totally nonsensical rules, but it’s actually exactly what you want in order to do a fast merge. The different stages represent the "result tree" (stage 0, aka "merged"), the original tree (stage 1, aka "orig"), and the two trees you are trying to merge (stage 2 and 3 respectively).

The order of stages 1, 2 and 3 (hence the order of three command-line arguments) are significant when you start a 3-way merge with an index file that is already populated. Here is an outline of how the algorithm works:

You would normally use git merge-index with supplied_git merge-one-file_ to do this last step. The script updates the files in the working tree as it merges each path and at the end of a successful merge.

When you start a 3-way merge with an index file that is already populated, it is assumed that it represents the state of the files in your work tree, and you can even have files with changes unrecorded in the index file. It is further assumed that this state is "derived" from the stage 2 tree. The 3-way merge refuses to run if it finds an entry in the original index file that does not match stage 2.

This is done to prevent you from losing your work-in-progress changes, and mixing your random changes in an unrelated merge commit. To illustrate, suppose you start from what has been committed last to your repository:

$ JC=git rev-parse --verify "HEAD^0" $ git checkout-index -f -u -a $JC

You do random edits, without running git update-index. And then you notice that the tip of your "upstream" tree has advanced since you pulled from him:

$ git fetch git://.... linus $ LT=git rev-parse FETCH_HEAD

Your work tree is still based on your HEAD ($JC), but you have some edits since. Three-way merge makes sure that you have not added or modified index entries since $JC, and if you haven’t, then does the right thing. So with the following sequence:

$ git read-tree -m -u git merge-base <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>J</mi><mi>C</mi></mrow><annotation encoding="application/x-tex">JC </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.09618em;">J</span><span class="mord mathnormal" style="margin-right:0.07153em;">C</span></span></span></span>LT JCJC JCLT $ git merge-index git-merge-one-file -a $ echo "Merge with Linus" |
git commit-tree git write-tree -p JC−pJC -p JCpLT

what you would commit is a pure merge between JCandJC and JCandLT without your work-in-progress changes, and your work tree would be updated to the result of the merge.

However, if you have local changes in the working tree that would be overwritten by this merge, git read-tree will refuse to run to prevent your changes from being lost.

In other words, there is no need to worry about what exists only in the working tree. When you have local changes in a part of the project that is not involved in the merge, your changes do not interfere with the merge, and are kept intact. When theydo interfere, the merge does not even start (_git read-tree_complains loudly and fails without modifying anything). In such a case, you can simply continue doing what you were in the middle of doing, and when your working tree is ready (i.e. you have finished your work-in-progress), attempt the merge again.