Code

[PATCH] read-tree: save more user hassles during fast-forward.
[git.git] / Documentation / git-read-tree.txt
1 git-read-tree(1)
2 ================
3 v0.1, May 2005
5 NAME
6 ----
7 git-read-tree - Reads tree information into the directory cache
10 SYNOPSIS
11 --------
12 'git-read-tree' (<tree-ish> | -m <tree-ish1> [<tree-ish2> <tree-ish3>])"
14 DESCRIPTION
15 -----------
16 Reads the tree information given by <tree> into the directory cache,
17 but does not actually *update* any of the files it "caches". (see:
18 git-checkout-cache)
20 Optionally, it can merge a tree into the cache or perform a 3-way
21 merge.
23 Trivial merges are done by "git-read-tree" itself.  Only conflicting paths
24 will be in unmerged state when "git-read-tree" returns.
26 OPTIONS
27 -------
28 -m::
29         Perform a merge, not just a read
31 <tree-ish#>::
32         The id of the tree object(s) to be read/merged.
35 Merging
36 -------
37 If '-m' is specified, "git-read-tree" performs 2 kinds of merge, a single tree
38 merge if only 1 tree is given or a 3-way merge if 3 trees are
39 provided.
41 Single Tree Merge
42 ~~~~~~~~~~~~~~~~~
43 If only 1 tree is specified, git-read-tree operates as if the user did not
44 specify '-m', except that if the original cache has an entry for a
45 given pathname; and the contents of the path matches with the tree
46 being read, the stat info from the cache is used. (In other words, the
47 cache's stat()s take precedence over the merged tree's)
49 That means that if you do a "git-read-tree -m <newtree>" followed by a
50 "git-checkout-cache -f -a", the "git-checkout-cache" only checks out
51 the stuff that really changed.
53 This is used to avoid unnecessary false hits when "git-diff-files" is
54 run after git-read-tree.
57 Two Tree Merge
58 ~~~~~~~~~~~~~~
60 Typically, this is invoked as "git-read-tree -m $H $M", where $H
61 is the head commit of the current repository, and $M is the head
62 of a foreign tree, which is simply ahead of $H (i.e. we are in a
63 fast forward situation).
65 When two trees are specified, the user is telling git-read-tree
66 the following:
68     (1) The current index and work tree is derived from $H, but
69         the user may have local changes in them since $H;
71     (2) The user wants to fast-forward to $M.
73 In this case, the "git-read-tree -m $H $M" command makes sure
74 that no local change is lost as the result of this "merge".
75 Here are the "carry forward" rules:
77         I (index)           H        M        Result
78        -------------------------------------------------------
79       0 nothing             nothing  nothing  (does not happen)
80       1 nothing             nothing  exists   use M
81       2 nothing             exists   nothing  remove path from cache
82       3 nothing             exists   exists   use M
84         clean I==H  I==M
85        ------------------
86       4 yes   N/A   N/A     nothing  nothing  keep index
87       5 no    N/A   N/A     nothing  nothing  keep index
89       6 yes   N/A   yes     nothing  exists   keep index
90       7 no    N/A   yes     nothing  exists   keep index
91       8 yes   N/A   no      nothing  exists   fail
92       9 no    N/A   no      nothing  exists   fail
94      10 yes   yes   N/A     exists   nothing  remove path from cache
95      11 no    yes   N/A     exists   nothing  fail
96      12 yes   no    N/A     exists   nothing  fail
97      13 no    no    N/A     exists   nothing  fail
99         clean (H=M)
100        ------
101      14 yes                 exists   exists   keep index
102      15 no                  exists   exists   keep index
104         clean I==H  I==M (H!=M)
105        ------------------
106      16 yes   no    no      exists   exists   fail
107      17 no    no    no      exists   exists   fail
108      18 yes   no    yes     exists   exists   keep index
109      19 no    no    yes     exists   exists   keep index
110      20 yes   yes   no      exists   exists   use M
111      21 no    yes   no      exists   exists   fail
113 In all "keep index" cases, the cache entry stays as in the
114 original index file.  If the entry were not up to date,
115 git-read-tree keeps the copy in the work tree intact when
116 operating under the -u flag.
118 When this form of git-read-tree returns successfully, you can
119 see what "local changes" you made are carried forward by running
120 "git-diff-cache --cached $M".  Note that this does not
121 necessarily match "git-diff-cache --cached $H" would have
122 produced before such a two tree merge.  This is because of cases
123 18 and 19 --- if you already had the changes in $M (e.g. maybe
124 you picked it up via e-mail in a patch form), "git-diff-cache
125 --cached $H" would have told you about the change before this
126 merge, but it would not show in "git-diff-cache --cached $M"
127 output after two-tree merge.
130 3-Way Merge
131 ~~~~~~~~~~~
132 Each "index" entry has two bits worth of "stage" state. stage 0 is the
133 normal one, and is the only one you'd see in any kind of normal use.
135 However, when you do "git-read-tree" with three trees, the "stage"
136 starts out at 1.
138 This means that you can do
140         git-read-tree -m <tree1> <tree2> <tree3>
142 and you will end up with an index with all of the <tree1> entries in
143 "stage1", all of the <tree2> entries in "stage2" and all of the
144 <tree3> entries in "stage3".
146 Furthermore, "git-read-tree" has special-case logic that says: if you see
147 a file that matches in all respects in the following states, it
148 "collapses" back to "stage0":
150    - stage 2 and 3 are the same; take one or the other (it makes no
151      difference - the same work has been done on stage 2 and 3)
153    - stage 1 and stage 2 are the same and stage 3 is different; take
154      stage 3 (some work has been done on stage 3)
156    - stage 1 and stage 3 are the same and stage 2 is different take
157      stage 2 (some work has been done on stage 2)
159 The "git-write-tree" command refuses to write a nonsensical tree, and it
160 will complain about unmerged entries if it sees a single entry that is not
161 stage 0.
163 Ok, this all sounds like a collection of totally nonsensical rules,
164 but it's actually exactly what you want in order to do a fast
165 merge. The different stages represent the "result tree" (stage 0, aka
166 "merged"), the original tree (stage 1, aka "orig"), and the two trees
167 you are trying to merge (stage 2 and 3 respectively).
169 In fact, the way "git-read-tree" works, it's entirely agnostic about how
170 you assign the stages, and you could really assign them any which way,
171 and the above is just a suggested way to do it (except since
172 "git-write-tree" refuses to write anything but stage0 entries, it makes
173 sense to always consider stage 0 to be the "full merge" state).
175 So what happens? Try it out. Select the original tree, and two trees
176 to merge, and look how it works:
178 - if a file exists in identical format in all three trees, it will
179   automatically collapse to "merged" state by the new git-read-tree.
181 - a file that has _any_ difference what-so-ever in the three trees
182   will stay as separate entries in the index. It's up to "script
183   policy" to determine how to remove the non-0 stages, and insert a
184   merged version.  But since the index is always sorted, they're easy
185   to find: they'll be clustered together.
187 - the index file saves and restores with all this information, so you
188   can merge things incrementally, but as long as it has entries in
189   stages 1/2/3 (ie "unmerged entries") you can't write the result. So
190   now the merge algorithm ends up being really simple:
192   * you walk the index in order, and ignore all entries of stage 0,
193     since they've already been done.
195   * if you find a "stage1", but no matching "stage2" or "stage3", you
196     know it's been removed from both trees (it only existed in the
197     original tree), and you remove that entry.
199   * if you find a matching "stage2" and "stage3" tree, you remove one
200     of them, and turn the other into a "stage0" entry. Remove any
201     matching "stage1" entry if it exists too.  .. all the normal
202     trivial rules ..
204 Incidentally - it also means that you don't even have to have a
205 separate subdirectory for this. All the information literally is in
206 the index file, which is a temporary thing anyway. There is no need to
207 worry about what is in the working directory, since it is never shown
208 and never used.
210 See Also
211 --------
212 link:git-write-tree.html[git-write-tree]; link:git-ls-files.html[git-ls-files]
215 Author
216 ------
217 Written by Linus Torvalds <torvalds@osdl.org>
219 Documentation
220 --------------
221 Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
223 GIT
224 ---
225 Part of the link:git.html[git] suite