Git - git-read-tree Documentation (2023)

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

Single Tree Merge

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

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

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

Two Tree Merge

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

When two trees are specified, the user is telling git read-treethe following:

  1. The current index and work tree is derived from $H, butthe user may have local changes in them since $H.

  2. The user wants to fast-forward to $M.

In this case, the git read-tree -m $H $M command makes surethat 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 failclean (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 theoriginal index file. If the entry is not up to date,git read-tree keeps the copy in the work tree intact whenoperating under the -u flag.

When this form of git read-tree returns successfully, you cansee which of the "local changes" that you made were carried forward by runninggit diff-index --cached $M. Note that this does notnecessarily match what git diff-index --cached $H would haveproduced before such a two tree merge. This is because of cases18 and 19 — if you already had the changes in $M (e.g. maybeyou picked it up via e-mail in a patch form), git diff-index--cached $H would have told you about the change before thismerge, 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 thisrule logically should be to remove the path if the user staged the removalof the path and then switching to a new branch. That however will preventthe initial checkout from happening, so the rule is modified to use M (newtree) only when the content of the index is empty. Otherwise the removalof the path is kept as long as $H and $M are the same.

3-Way Merge

Each "index" entry has two bits worth of "stage" state. stage 0 is thenormal 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 <tree1> <tree2> <tree3>

and you will end up with an index with all of the <tree1> entries in"stage1", all of the <tree2> entries in "stage2" and all of the<tree3> entries in "stage3". When performing a merge of anotherbranch into the current branch, we use the common ancestor treeas <tree1>, the current branch head as <tree2>, and the otherbranch head as <tree3>.

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

  • stage 2 and 3 are the same; take one or the other (it makes nodifference - the same work has been done on our branch instage 2 and their branch in stage 3)

  • stage 1 and stage 2 are the same and stage 3 is different; takestage 3 (our branch in stage 2 did not do anything since theancestor in stage 1 while their branch in stage 3 worked onit)

  • stage 1 and stage 3 are the same and stage 2 is different takestage 2 (we did something while they did nothing)

The git write-tree command refuses to write a nonsensical tree, and itwill complain about unmerged entries if it sees a single entry that is notstage 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 fastmerge. The different stages represent the "result tree" (stage 0, aka"merged"), the original tree (stage 1, aka "orig"), and the two treesyou are trying to merge (stage 2 and 3 respectively).

The order of stages 1, 2 and 3 (hence the order of three<tree-ish> command-line arguments) are significant when youstart a 3-way merge with an index file that is alreadypopulated. Here is an outline of how the algorithm works:

  • if a file exists in identical format in all three trees, it willautomatically collapse to "merged" state by git read-tree.

  • a file that has any difference what-so-ever in the three treeswill stay as separate entries in the index. It’s up to "porcelainpolicy" to determine how to remove the non-0 stages, and insert amerged version.

  • the index file saves and restores with all this information, so youcan merge things incrementally, but as long as it has entries instages 1/2/3 (i.e., "unmerged entries") you can’t write the result. Sonow the merge algorithm ends up being really simple:

    • you walk the index in order, and ignore all entries of stage 0,since they’ve already been done.

    • if you find a "stage1", but no matching "stage2" or "stage3", youknow it’s been removed from both trees (it only existed in theoriginal tree), and you remove that entry.

    • if you find a matching "stage2" and "stage3" tree, you remove oneof them, and turn the other into a "stage0" entry. Remove anymatching "stage1" entry if it exists too. .. all the normaltrivial rules ..

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

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

This is done to prevent you from losing your work-in-progresschanges, and mixing your random changes in an unrelated mergecommit. To illustrate, suppose you start from what has beencommitted 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 thenyou notice that the tip of your "upstream" tree has advancedsince 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 havesome edits since. Three-way merge makes sure that you have notadded 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 $JC $LT` $JC $LT$ git merge-index git-merge-one-file -a$ echo "Merge with Linus" | \ git commit-tree `git write-tree` -p $JC -p $LT

what you would commit is a pure merge between $JC and $LT withoutyour work-in-progress changes, and your work tree would beupdated to the result of the merge.

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

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

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated: 12/10/2023

Views: 6414

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.