Documentation / git-read-tree.txton commit Merge branch 'fixes' (0493a3f)
   1git-read-tree(1)
   2================
   3v0.1, May 2005
   4
   5NAME
   6----
   7git-read-tree - Reads tree information into the directory cache
   8
   9
  10SYNOPSIS
  11--------
  12'git-read-tree' (<tree-ish> | [-m [-u|-i]] <tree-ish1> [<tree-ish2> [<tree-ish3>]])
  13
  14
  15DESCRIPTION
  16-----------
  17Reads the tree information given by <tree-ish> into the directory cache,
  18but does not actually *update* any of the files it "caches". (see:
  19git-checkout-index)
  20
  21Optionally, it can merge a tree into the cache, perform a
  22fast-forward (i.e. 2-way) merge, or a 3-way merge, with the -m
  23flag.  When used with -m, the -u flag causes it to also update
  24the files in the work tree with the result of the merge.
  25
  26Trivial merges are done by "git-read-tree" itself.  Only conflicting paths
  27will be in unmerged state when "git-read-tree" returns.
  28
  29OPTIONS
  30-------
  31-m::
  32        Perform a merge, not just a read.
  33
  34-u::
  35        After a successful merge, update the files in the work
  36        tree with the result of the merge.
  37
  38-i::
  39        Usually a merge requires the index file as well as the
  40        files in the working tree are up to date with the
  41        current head commit, in order not to lose local
  42        changes.  This flag disables the check with the working
  43        tree and is meant to be used when creating a merge of
  44        trees that are not directly related to the current
  45        working tree status into a temporary index file.
  46
  47
  48<tree-ish#>::
  49        The id of the tree object(s) to be read/merged.
  50
  51
  52Merging
  53-------
  54If '-m' is specified, "git-read-tree" can perform 3 kinds of
  55merge, a single tree merge if only 1 tree is given, a
  56fast-forward merge with 2 trees, or a 3-way merge if 3 trees are
  57provided.
  58
  59
  60Single Tree Merge
  61~~~~~~~~~~~~~~~~~
  62If only 1 tree is specified, git-read-tree operates as if the user did not
  63specify '-m', except that if the original cache has an entry for a
  64given pathname, and the contents of the path matches with the tree
  65being read, the stat info from the cache is used. (In other words, the
  66cache's stat()s take precedence over the merged tree's).
  67
  68That means that if you do a "git-read-tree -m <newtree>" followed by a
  69"git-checkout-index -f -u -a", the "git-checkout-index" only checks out
  70the stuff that really changed.
  71
  72This is used to avoid unnecessary false hits when "git-diff-files" is
  73run after git-read-tree.
  74
  75
  76Two Tree Merge
  77~~~~~~~~~~~~~~
  78
  79Typically, this is invoked as "git-read-tree -m $H $M", where $H
  80is the head commit of the current repository, and $M is the head
  81of a foreign tree, which is simply ahead of $H (i.e. we are in a
  82fast forward situation).
  83
  84When two trees are specified, the user is telling git-read-tree
  85the following:
  86
  87    (1) The current index and work tree is derived from $H, but
  88        the user may have local changes in them since $H;
  89
  90    (2) The user wants to fast-forward to $M.
  91
  92In this case, the "git-read-tree -m $H $M" command makes sure
  93that no local change is lost as the result of this "merge".
  94Here are the "carry forward" rules:
  95
  96        I (index)           H        M        Result
  97       -------------------------------------------------------
  98      0 nothing             nothing  nothing  (does not happen)
  99      1 nothing             nothing  exists   use M
 100      2 nothing             exists   nothing  remove path from cache
 101      3 nothing             exists   exists   use M
 102
 103        clean I==H  I==M
 104       ------------------
 105      4 yes   N/A   N/A     nothing  nothing  keep index
 106      5 no    N/A   N/A     nothing  nothing  keep index
 107
 108      6 yes   N/A   yes     nothing  exists   keep index
 109      7 no    N/A   yes     nothing  exists   keep index
 110      8 yes   N/A   no      nothing  exists   fail
 111      9 no    N/A   no      nothing  exists   fail
 112
 113     10 yes   yes   N/A     exists   nothing  remove path from cache
 114     11 no    yes   N/A     exists   nothing  fail
 115     12 yes   no    N/A     exists   nothing  fail
 116     13 no    no    N/A     exists   nothing  fail
 117
 118        clean (H=M)
 119       ------
 120     14 yes                 exists   exists   keep index
 121     15 no                  exists   exists   keep index
 122
 123        clean I==H  I==M (H!=M)
 124       ------------------
 125     16 yes   no    no      exists   exists   fail
 126     17 no    no    no      exists   exists   fail
 127     18 yes   no    yes     exists   exists   keep index
 128     19 no    no    yes     exists   exists   keep index
 129     20 yes   yes   no      exists   exists   use M
 130     21 no    yes   no      exists   exists   fail
 131
 132In all "keep index" cases, the cache entry stays as in the
 133original index file.  If the entry were not up to date,
 134git-read-tree keeps the copy in the work tree intact when
 135operating under the -u flag.
 136
 137When this form of git-read-tree returns successfully, you can
 138see what "local changes" you made are carried forward by running
 139"git-diff-index --cached $M".  Note that this does not
 140necessarily match "git-diff-index --cached $H" would have
 141produced before such a two tree merge.  This is because of cases
 14218 and 19 --- if you already had the changes in $M (e.g. maybe
 143you picked it up via e-mail in a patch form), "git-diff-index
 144--cached $H" would have told you about the change before this
 145merge, but it would not show in "git-diff-index --cached $M"
 146output after two-tree merge.
 147
 148
 1493-Way Merge
 150~~~~~~~~~~~
 151Each "index" entry has two bits worth of "stage" state. stage 0 is the
 152normal one, and is the only one you'd see in any kind of normal use.
 153
 154However, when you do "git-read-tree" with three trees, the "stage"
 155starts out at 1.
 156
 157This means that you can do
 158
 159        git-read-tree -m <tree1> <tree2> <tree3>
 160
 161and you will end up with an index with all of the <tree1> entries in
 162"stage1", all of the <tree2> entries in "stage2" and all of the
 163<tree3> entries in "stage3".
 164
 165Furthermore, "git-read-tree" has special-case logic that says: if you see
 166a file that matches in all respects in the following states, it
 167"collapses" back to "stage0":
 168
 169   - stage 2 and 3 are the same; take one or the other (it makes no
 170     difference - the same work has been done on stage 2 and 3)
 171
 172   - stage 1 and stage 2 are the same and stage 3 is different; take
 173     stage 3 (some work has been done on stage 3)
 174
 175   - stage 1 and stage 3 are the same and stage 2 is different take
 176     stage 2 (some work has been done on stage 2)
 177
 178The "git-write-tree" command refuses to write a nonsensical tree, and it
 179will complain about unmerged entries if it sees a single entry that is not
 180stage 0.
 181
 182Ok, this all sounds like a collection of totally nonsensical rules,
 183but it's actually exactly what you want in order to do a fast
 184merge. The different stages represent the "result tree" (stage 0, aka
 185"merged"), the original tree (stage 1, aka "orig"), and the two trees
 186you are trying to merge (stage 2 and 3 respectively).
 187
 188The order of stages 1, 2 and 3 (hence the order of three
 189<tree-ish> command line arguments) are significant when you
 190start a 3-way merge with an index file that is already
 191populated.  Here is an outline of how the algorithm works:
 192
 193- if a file exists in identical format in all three trees, it will
 194  automatically collapse to "merged" state by git-read-tree.
 195
 196- a file that has _any_ difference what-so-ever in the three trees
 197  will stay as separate entries in the index. It's up to "porcelain
 198  policy" to determine how to remove the non-0 stages, and insert a
 199  merged version.
 200
 201- the index file saves and restores with all this information, so you
 202  can merge things incrementally, but as long as it has entries in
 203  stages 1/2/3 (ie "unmerged entries") you can't write the result. So
 204  now the merge algorithm ends up being really simple:
 205
 206  * you walk the index in order, and ignore all entries of stage 0,
 207    since they've already been done.
 208
 209  * if you find a "stage1", but no matching "stage2" or "stage3", you
 210    know it's been removed from both trees (it only existed in the
 211    original tree), and you remove that entry.
 212
 213  * if you find a matching "stage2" and "stage3" tree, you remove one
 214    of them, and turn the other into a "stage0" entry. Remove any
 215    matching "stage1" entry if it exists too.  .. all the normal
 216    trivial rules ..
 217
 218You would normally use "git-merge-index" with supplied
 219"git-merge-one-file" to do this last step.  The script
 220does not touch the files in the work tree, and the entire merge
 221happens in the index file.  In other words, there is no need to
 222worry about what is in the working directory, since it is never
 223shown and never used.
 224
 225When you start a 3-way merge with an index file that is already
 226populated, it is assumed that it represents the state of the
 227files in your work tree, and you can even have files with
 228changes unrecorded in the index file.  It is further assumed
 229that this state is "derived" from the stage 2 tree.  The 3-way
 230merge refuses to run if it finds an entry in the original index
 231file that does not match stage 2.
 232
 233This is done to prevent you from losing your work-in-progress
 234changes.  To illustrate, suppose you start from what has been
 235commited last to your repository:
 236
 237    $ JC=`cat .git/HEAD`
 238    $ git-checkout-index -f -u -a $JC
 239
 240You do random edits, without running git-update-index.  And then
 241you notice that the tip of your "upstream" tree has advanced
 242since you pulled from him:
 243
 244    $ git-fetch rsync://.... linus
 245    $ LT=`cat .git/MERGE_HEAD`
 246
 247Your work tree is still based on your HEAD ($JC), but you have
 248some edits since.  Three-way merge makes sure that you have not
 249added or modified cache entries since $JC, and if you haven't,
 250then does the right thing.  So with the following sequence:
 251
 252    $ git-read-tree -m -u `git-merge-base $JC $LT` $JC $LT
 253    $ git-merge-index git-merge-one-file -a
 254    $ echo "Merge with Linus" | \
 255      git-commit-tree `git-write-tree` -p $JC -p $LT
 256
 257what you would commit is a pure merge between $JC and LT without
 258your work-in-progress changes, and your work tree would be
 259updated to the result of the merge.
 260
 261
 262See Also
 263--------
 264gitlink:git-write-tree[1]; gitlink:git-ls-files[1]
 265
 266
 267Author
 268------
 269Written by Linus Torvalds <torvalds@osdl.org>
 270
 271Documentation
 272--------------
 273Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
 274
 275GIT
 276---
 277Part of the gitlink:git[7] suite
 278