NAME

git-read-tree - Reads tree information into the index

SYNOPSIS

  1. git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>]
  2. [-u [--exclude-per-directory=<gitignore>] | -i]]
  3. [--index-output=<file>] [--no-sparse-checkout]
  4. (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])

DESCRIPTION

Reads the tree information given by <tree-ish> into the index,but does not actually update any of the files it "caches". (see:git-checkout-index[1])

Optionally, it can merge a tree into the index, perform afast-forward (i.e. 2-way) merge, or a 3-way merge, with the -mflag. When used with -m, the -u flag causes it to also updatethe files in the work tree with the result of the merge.

Trivial merges are done by git read-tree itself. Only conflicting pathswill be in unmerged state when git read-tree returns.

OPTIONS

  • -m
  • Perform a merge, not just a read. The command willrefuse to run if your index file has unmerged entries,indicating that you have not finished previous merge youstarted.

  • —reset

  • Same as -m, except that unmerged entries are discarded insteadof failing. When used with -u, updates leading to loss ofworking tree changes will not abort the operation.

  • -u

  • After a successful merge, update the files in the worktree with the result of the merge.

  • -i

  • Usually a merge requires the index file as well as thefiles in the working tree to be up to date with thecurrent head commit, in order not to lose localchanges. This flag disables the check with the workingtree and is meant to be used when creating a merge oftrees that are not directly related to the currentworking tree status into a temporary index file.

  • -n

  • —dry-run
  • Check if the command would error out, without updating the indexor the files in the working tree for real.

  • -v

  • Show the progress of checking files out.

  • —trivial

  • Restrict three-way merge by git read-tree to happenonly if there is no file-level merging required, insteadof resolving merge for trivial cases and leavingconflicting files unresolved in the index.

  • —aggressive

  • Usually a three-way merge by git read-tree resolvesthe merge for really trivial cases and leaves othercases unresolved in the index, so that porcelains canimplement different merge policies. This flag makes thecommand resolve a few more cases internally:
  • when one side removes a path and the other side leaves the pathunmodified. The resolution is to remove that path.

  • when both sides remove a path. The resolution is to remove that path.

  • when both sides add a path identically. The resolutionis to add that path.

  • —prefix=
  • Keep the current index contents, and read the contentsof the named tree-ish under the directory at <prefix>.The command will refuse to overwrite entries that alreadyexisted in the original index file.

  • —exclude-per-directory=

  • When running the command with -u and -m options, themerge result may need to overwrite paths that are nottracked in the current branch. The command usuallyrefuses to proceed with the merge to avoid losing such apath. However this safety valve sometimes gets in theway. For example, it often happens that the otherbranch added a file that used to be a generated file inyour branch, and the safety valve triggers when you tryto switch to that branch after you ran make but beforerunning make clean to remove the generated file. Thisoption tells the command to read per-directory excludefile (usually .gitignore) and allows such an untrackedbut explicitly ignored file to be overwritten.

  • —index-output=

  • Instead of writing the results out to $GIT_INDEX_FILE,write the resulting index in the named file. While thecommand is operating, the original index file is lockedwith the same mechanism as usual. The file must allowto be rename(2)ed into from a temporary file that iscreated next to the usual index file; typically thismeans it needs to be on the same filesystem as the indexfile itself, and you need write permission to thedirectories the index file and index output file arelocated in.

  • —[no-]recurse-submodules

  • Using —recurse-submodules will update the content of all initializedsubmodules according to the commit recorded in the superproject bycalling read-tree recursively, also setting the submodules HEAD to bedetached at that commit.

  • —no-sparse-checkout

  • Disable sparse checkout support even if core.sparseCheckoutis true.

  • —empty

  • Instead of reading tree object(s) into the index, just emptyit.

  • -q

  • —quiet
  • Quiet, suppress feedback messages.

  • The id of the tree object(s) to be read/merged.

MERGING

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-tree_the following:

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

  • 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:

  1. I H M Result

  1. 0 nothing nothing nothing (does not happen)
  2. 1 nothing nothing exists use M
  3. 2 nothing exists nothing remove path from index
  4. 3 nothing exists exists, use M if &#34;initial checkout&#34;,
  5. H == M keep index otherwise
  6. exists, fail
  7. H != M
  8. clean I==H I==M
  9. ------------------
  10. 4 yes N/A N/A nothing nothing keep index
  11. 5 no N/A N/A nothing nothing keep index
  12. 6 yes N/A yes nothing exists keep index
  13. 7 no N/A yes nothing exists keep index
  14. 8 yes N/A no nothing exists fail
  15. 9 no N/A no nothing exists fail
  16. 10 yes yes N/A exists nothing remove path from index
  17. 11 no yes N/A exists nothing fail
  18. 12 yes no N/A exists nothing fail
  19. 13 no no N/A exists nothing fail
  20. clean (H==M)
  21. ------
  22. 14 yes exists exists keep index
  23. 15 no exists exists keep index
  24. clean I==H I==M (H!=M)
  25. ------------------
  26. 16 yes no no exists exists fail
  27. 17 no no no exists exists fail
  28. 18 yes no yes exists exists keep index
  29. 19 no no yes exists exists keep index
  30. 20 yes yes no exists exists use M
  31. 21 no yes no exists exists fail</pre>

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

  1. $ 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:

  1. $ JC=`git rev-parse --verify "HEAD^0"`
  2. $ 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:

  1. $ git fetch git://.... linus
  2. $ 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:

  1. $ git read-tree -m -u `git merge-base $JC $LT` $JC $LT
  2. $ git merge-index git-merge-one-file -a
  3. $ echo "Merge with Linus" | \
  4. 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-tree_complains 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.

SPARSE CHECKOUT

"Sparse checkout" allows populating the working directory sparsely.It uses the skip-worktree bit (see git-update-index[1]) to tellGit whether a file in the working directory is worth looking at.

git read-tree and other merge-based commands (git merge, gitcheckout…​) can help maintaining the skip-worktree bitmap and workingdirectory update. $GITDIR/info/sparse-checkout is used todefine the skip-worktree reference bitmap. When _git read-tree needsto update the working directory, it resets the skip-worktree bit in the indexbased on this file, which uses the same syntax as .gitignore files.If an entry matches a pattern in this file, skip-worktree will not beset on that entry. Otherwise, skip-worktree will be set.

Then it compares the new skip-worktree value with the previous one. Ifskip-worktree turns from set to unset, it will add the correspondingfile back. If it turns from unset to set, that file will be removed.

While $GITDIR/info/sparse-checkout is usually used to specify whatfiles are in, you can also specify what files are _not in, usingnegate patterns. For example, to remove the file unwanted:

  1. /*
  2. !unwanted

Another tricky thing is fully repopulating the working directory when youno longer want sparse checkout. You cannot just disable "sparsecheckout" because skip-worktree bits are still in the index and your workingdirectory is still sparsely populated. You should re-populate the workingdirectory with the $GIT_DIR/info/sparse-checkout file content asfollows:

  1. /*

Then you can disable sparse checkout. Sparse checkout support in gitread-tree and similar commands is disabled by default. You need toturn core.sparseCheckout on in order to have sparse checkoutsupport.

SEE ALSO

git-write-tree[1]; git-ls-files[1];gitignore[5]

GIT

Part of the git[1] suite