David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 1 | git-read-tree(1) |
| 2 | ================ |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 3 | |
| 4 | NAME |
| 5 | ---- |
Lukas_Sandström | 5f3aa19 | 2005-11-11 02:12:27 +0100 | [diff] [blame] | 6 | git-read-tree - Reads tree information into the index |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 7 | |
| 8 | |
| 9 | SYNOPSIS |
| 10 | -------- |
Martin von Zweigbergk | 7791a1d | 2011-07-01 22:38:26 -0400 | [diff] [blame] | 11 | [verse] |
Ævar Arnfjörð Bjarmason | e8eeda1 | 2022-10-13 17:39:15 +0200 | [diff] [blame] | 12 | 'git read-tree' [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>) |
Elijah Newren | 491a757 | 2021-09-27 16:33:40 +0000 | [diff] [blame] | 13 | [-u | -i]] [--index-output=<file>] [--no-sparse-checkout] |
Jan Krüger | fb1bb96 | 2010-09-10 15:28:59 +0200 | [diff] [blame] | 14 | (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]]) |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 15 | |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 16 | |
| 17 | DESCRIPTION |
| 18 | ----------- |
Lukas_Sandström | 5f3aa19 | 2005-11-11 02:12:27 +0100 | [diff] [blame] | 19 | Reads the tree information given by <tree-ish> into the index, |
David Greaves | c1bdacf | 2005-05-10 22:32:38 +0100 | [diff] [blame] | 20 | but does not actually *update* any of the files it "caches". (see: |
Dan McGee | 5162e69 | 2007-12-29 00:20:38 -0600 | [diff] [blame] | 21 | linkgit:git-checkout-index[1]) |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 22 | |
Lukas_Sandström | 5f3aa19 | 2005-11-11 02:12:27 +0100 | [diff] [blame] | 23 | Optionally, it can merge a tree into the index, perform a |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 24 | fast-forward (i.e. 2-way) merge, or a 3-way merge, with the `-m` |
| 25 | flag. When used with `-m`, the `-u` flag causes it to also update |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 26 | the files in the work tree with the result of the merge. |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 27 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 28 | Trivial merges are done by 'git read-tree' itself. Only conflicting paths |
| 29 | will be in unmerged state when 'git read-tree' returns. |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 30 | |
| 31 | OPTIONS |
| 32 | ------- |
| 33 | -m:: |
No name | 3f41f5a | 2005-12-02 06:37:13 -0500 | [diff] [blame] | 34 | Perform a merge, not just a read. The command will |
| 35 | refuse to run if your index file has unmerged entries, |
| 36 | indicating that you have not finished previous merge you |
| 37 | started. |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 38 | |
Nikolai Weibull | 2db0bfb | 2005-11-15 00:20:01 +0100 | [diff] [blame] | 39 | --reset:: |
Nguyễn Thái Ngọc Duy | b5a0bd6 | 2019-04-01 19:05:05 +0700 | [diff] [blame] | 40 | Same as -m, except that unmerged entries are discarded instead |
Elijah Newren | 0e29222 | 2021-09-27 16:33:48 +0000 | [diff] [blame] | 41 | of failing. When used with `-u`, updates leading to loss of |
| 42 | working tree changes or untracked files or directories will not |
| 43 | abort the operation. |
Nikolai Weibull | 2db0bfb | 2005-11-15 00:20:01 +0100 | [diff] [blame] | 44 | |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 45 | -u:: |
| 46 | After a successful merge, update the files in the work |
| 47 | tree with the result of the merge. |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 48 | |
Junio C Hamano | f318dd2 | 2005-09-19 11:33:14 -0700 | [diff] [blame] | 49 | -i:: |
| 50 | Usually a merge requires the index file as well as the |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 51 | files in the working tree to be up to date with the |
Junio C Hamano | f318dd2 | 2005-09-19 11:33:14 -0700 | [diff] [blame] | 52 | current head commit, in order not to lose local |
| 53 | changes. This flag disables the check with the working |
| 54 | tree and is meant to be used when creating a merge of |
| 55 | trees that are not directly related to the current |
| 56 | working tree status into a temporary index file. |
| 57 | |
Jens Lehmann | ea5070c | 2011-05-25 22:10:41 +0200 | [diff] [blame] | 58 | -n:: |
| 59 | --dry-run:: |
| 60 | Check if the command would error out, without updating the index |
Justin Lebar | a58088a | 2014-03-31 15:11:44 -0700 | [diff] [blame] | 61 | or the files in the working tree for real. |
Jens Lehmann | ea5070c | 2011-05-25 22:10:41 +0200 | [diff] [blame] | 62 | |
Miklos Vajna | 22e801f | 2008-06-09 22:25:15 +0200 | [diff] [blame] | 63 | -v:: |
| 64 | Show the progress of checking files out. |
| 65 | |
Jakub Narebski | 6da0878 | 2007-06-16 21:03:45 +0200 | [diff] [blame] | 66 | --trivial:: |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 67 | Restrict three-way merge by 'git read-tree' to happen |
Jakub Narebski | 6da0878 | 2007-06-16 21:03:45 +0200 | [diff] [blame] | 68 | only if there is no file-level merging required, instead |
| 69 | of resolving merge for trivial cases and leaving |
| 70 | conflicting files unresolved in the index. |
| 71 | |
Junio C Hamano | afaa8d6 | 2006-03-02 01:11:05 -0800 | [diff] [blame] | 72 | --aggressive:: |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 73 | Usually a three-way merge by 'git read-tree' resolves |
Junio C Hamano | afaa8d6 | 2006-03-02 01:11:05 -0800 | [diff] [blame] | 74 | the merge for really trivial cases and leaves other |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 75 | cases unresolved in the index, so that porcelains can |
Junio C Hamano | afaa8d6 | 2006-03-02 01:11:05 -0800 | [diff] [blame] | 76 | implement different merge policies. This flag makes the |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 77 | command resolve a few more cases internally: |
Junio C Hamano | afaa8d6 | 2006-03-02 01:11:05 -0800 | [diff] [blame] | 78 | + |
| 79 | * when one side removes a path and the other side leaves the path |
| 80 | unmodified. The resolution is to remove that path. |
| 81 | * when both sides remove a path. The resolution is to remove that path. |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 82 | * when both sides add a path identically. The resolution |
Junio C Hamano | afaa8d6 | 2006-03-02 01:11:05 -0800 | [diff] [blame] | 83 | is to add that path. |
| 84 | |
Andreas G. Schacker | 30221a3 | 2018-01-09 16:30:34 +0100 | [diff] [blame] | 85 | --prefix=<prefix>:: |
Junio C Hamano | f4c6f2d | 2006-01-14 21:46:58 -0800 | [diff] [blame] | 86 | Keep the current index contents, and read the contents |
Clemens Buchacher | 5c951ef | 2011-12-31 12:50:56 +0100 | [diff] [blame] | 87 | of the named tree-ish under the directory at `<prefix>`. |
| 88 | The command will refuse to overwrite entries that already |
Andreas G. Schacker | 30221a3 | 2018-01-09 16:30:34 +0100 | [diff] [blame] | 89 | existed in the original index file. |
Junio C Hamano | f4c6f2d | 2006-01-14 21:46:58 -0800 | [diff] [blame] | 90 | |
Junio C Hamano | 5e7f56a | 2007-03-31 23:27:41 -0700 | [diff] [blame] | 91 | --index-output=<file>:: |
| 92 | Instead of writing the results out to `$GIT_INDEX_FILE`, |
| 93 | write the resulting index in the named file. While the |
| 94 | command is operating, the original index file is locked |
| 95 | with the same mechanism as usual. The file must allow |
| 96 | to be rename(2)ed into from a temporary file that is |
| 97 | created next to the usual index file; typically this |
| 98 | means it needs to be on the same filesystem as the index |
| 99 | file itself, and you need write permission to the |
| 100 | directories the index file and index output file are |
| 101 | located in. |
| 102 | |
Stefan Beller | 2580491 | 2017-03-14 14:46:42 -0700 | [diff] [blame] | 103 | --[no-]recurse-submodules:: |
Damien Robert | acbfae3 | 2020-04-06 15:57:09 +0200 | [diff] [blame] | 104 | Using --recurse-submodules will update the content of all active |
Stefan Beller | 2580491 | 2017-03-14 14:46:42 -0700 | [diff] [blame] | 105 | submodules according to the commit recorded in the superproject by |
Damien Robert | b3cec57 | 2020-04-06 15:57:06 +0200 | [diff] [blame] | 106 | calling read-tree recursively, also setting the submodules' HEAD to be |
Stefan Beller | 2580491 | 2017-03-14 14:46:42 -0700 | [diff] [blame] | 107 | detached at that commit. |
| 108 | |
Nguyễn Thái Ngọc Duy | a5d07d0 | 2009-08-20 20:47:11 +0700 | [diff] [blame] | 109 | --no-sparse-checkout:: |
| 110 | Disable sparse checkout support even if `core.sparseCheckout` |
| 111 | is true. |
| 112 | |
Jan Krüger | fb1bb96 | 2010-09-10 15:28:59 +0200 | [diff] [blame] | 113 | --empty:: |
| 114 | Instead of reading tree object(s) into the index, just empty |
| 115 | it. |
| 116 | |
Nguyễn Thái Ngọc Duy | 3e41485 | 2019-03-22 16:31:37 +0700 | [diff] [blame] | 117 | -q:: |
| 118 | --quiet:: |
| 119 | Quiet, suppress feedback messages. |
| 120 | |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 121 | <tree-ish#>:: |
| 122 | The id of the tree object(s) to be read/merged. |
| 123 | |
| 124 | |
Nguyễn Thái Ngọc Duy | 76a8788 | 2018-04-30 17:35:33 +0200 | [diff] [blame] | 125 | MERGING |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 126 | ------- |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 127 | If `-m` is specified, 'git read-tree' can perform 3 kinds of |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 128 | merge, a single tree merge if only 1 tree is given, a |
Jean-Noel Avila | 9932242 | 2017-05-11 14:06:33 +0200 | [diff] [blame] | 129 | fast-forward merge with 2 trees, or a 3-way merge if 3 or more trees are |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 130 | provided. |
| 131 | |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 132 | |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 133 | Single Tree Merge |
| 134 | ~~~~~~~~~~~~~~~~~ |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 135 | If only 1 tree is specified, 'git read-tree' operates as if the user did not |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 136 | specify `-m`, except that if the original index has an entry for a |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 137 | given pathname, and the contents of the path match with the tree |
Lukas_Sandström | 5f3aa19 | 2005-11-11 02:12:27 +0100 | [diff] [blame] | 138 | being read, the stat info from the index is used. (In other words, the |
| 139 | index's stat()s take precedence over the merged tree's). |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 140 | |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 141 | That means that if you do a `git read-tree -m <newtree>` followed by a |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 142 | `git checkout-index -f -u -a`, the 'git checkout-index' only checks out |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 143 | the stuff that really changed. |
| 144 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 145 | This is used to avoid unnecessary false hits when 'git diff-files' is |
| 146 | run after 'git read-tree'. |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 147 | |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 148 | |
| 149 | Two Tree Merge |
| 150 | ~~~~~~~~~~~~~~ |
| 151 | |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 152 | Typically, this is invoked as `git read-tree -m $H $M`, where $H |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 153 | is the head commit of the current repository, and $M is the head |
| 154 | of a foreign tree, which is simply ahead of $H (i.e. we are in a |
Felipe Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 155 | fast-forward situation). |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 156 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 157 | When two trees are specified, the user is telling 'git read-tree' |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 158 | the following: |
| 159 | |
Jonas Fonseca | df8baa4 | 2005-10-03 19:16:30 +0200 | [diff] [blame] | 160 | 1. The current index and work tree is derived from $H, but |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 161 | the user may have local changes in them since $H. |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 162 | |
Jonas Fonseca | df8baa4 | 2005-10-03 19:16:30 +0200 | [diff] [blame] | 163 | 2. The user wants to fast-forward to $M. |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 164 | |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 165 | In this case, the `git read-tree -m $H $M` command makes sure |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 166 | that no local change is lost as the result of this "merge". |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 167 | Here are the "carry forward" rules, where "I" denotes the index, |
| 168 | "clean" means that index and work tree coincide, and "exists"/"nothing" |
| 169 | refer to the presence of a path in the specified commit: |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 170 | |
Jeff King | c08fd63 | 2017-09-23 01:55:26 -0400 | [diff] [blame] | 171 | .... |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 172 | I H M Result |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 173 | ------------------------------------------------------- |
Michael J Gruber | 71928f7 | 2010-03-15 11:54:45 +0100 | [diff] [blame] | 174 | 0 nothing nothing nothing (does not happen) |
| 175 | 1 nothing nothing exists use M |
| 176 | 2 nothing exists nothing remove path from index |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 177 | 3 nothing exists exists, use M if "initial checkout", |
Junio C Hamano | 5521883 | 2008-09-07 19:49:25 -0700 | [diff] [blame] | 178 | H == M keep index otherwise |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 179 | exists, fail |
Junio C Hamano | 5521883 | 2008-09-07 19:49:25 -0700 | [diff] [blame] | 180 | H != M |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 181 | |
| 182 | clean I==H I==M |
| 183 | ------------------ |
Michael J Gruber | 71928f7 | 2010-03-15 11:54:45 +0100 | [diff] [blame] | 184 | 4 yes N/A N/A nothing nothing keep index |
| 185 | 5 no N/A N/A nothing nothing keep index |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 186 | |
Michael J Gruber | 71928f7 | 2010-03-15 11:54:45 +0100 | [diff] [blame] | 187 | 6 yes N/A yes nothing exists keep index |
| 188 | 7 no N/A yes nothing exists keep index |
| 189 | 8 yes N/A no nothing exists fail |
| 190 | 9 no N/A no nothing exists fail |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 191 | |
Lukas_Sandström | 5f3aa19 | 2005-11-11 02:12:27 +0100 | [diff] [blame] | 192 | 10 yes yes N/A exists nothing remove path from index |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 193 | 11 no yes N/A exists nothing fail |
| 194 | 12 yes no N/A exists nothing fail |
| 195 | 13 no no N/A exists nothing fail |
| 196 | |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 197 | clean (H==M) |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 198 | ------ |
| 199 | 14 yes exists exists keep index |
| 200 | 15 no exists exists keep index |
| 201 | |
| 202 | clean I==H I==M (H!=M) |
| 203 | ------------------ |
| 204 | 16 yes no no exists exists fail |
| 205 | 17 no no no exists exists fail |
| 206 | 18 yes no yes exists exists keep index |
| 207 | 19 no no yes exists exists keep index |
| 208 | 20 yes yes no exists exists use M |
| 209 | 21 no yes no exists exists fail |
Jeff King | c08fd63 | 2017-09-23 01:55:26 -0400 | [diff] [blame] | 210 | .... |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 211 | |
Lukas_Sandström | 5f3aa19 | 2005-11-11 02:12:27 +0100 | [diff] [blame] | 212 | In all "keep index" cases, the index entry stays as in the |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 213 | original index file. If the entry is not up to date, |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 214 | 'git read-tree' keeps the copy in the work tree intact when |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 215 | operating under the -u flag. |
| 216 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 217 | When this form of 'git read-tree' returns successfully, you can |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 218 | see which of the "local changes" that you made were carried forward by running |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 219 | `git diff-index --cached $M`. Note that this does not |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 220 | necessarily match what `git diff-index --cached $H` would have |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 221 | produced before such a two tree merge. This is because of cases |
| 222 | 18 and 19 --- if you already had the changes in $M (e.g. maybe |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 223 | you picked it up via e-mail in a patch form), `git diff-index |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 224 | --cached $H` would have told you about the change before this |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 225 | merge, but it would not show in `git diff-index --cached $M` |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 226 | output after the two-tree merge. |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 227 | |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 228 | Case 3 is slightly tricky and needs explanation. The result from this |
Junio C Hamano | 5521883 | 2008-09-07 19:49:25 -0700 | [diff] [blame] | 229 | rule logically should be to remove the path if the user staged the removal |
Ralf Wildenhues | 79fd4cc | 2008-09-29 22:30:00 +0200 | [diff] [blame] | 230 | of the path and then switching to a new branch. That however will prevent |
Junio C Hamano | 5521883 | 2008-09-07 19:49:25 -0700 | [diff] [blame] | 231 | the initial checkout from happening, so the rule is modified to use M (new |
Michael J Gruber | 7325283 | 2010-03-15 11:54:46 +0100 | [diff] [blame] | 232 | tree) only when the content of the index is empty. Otherwise the removal |
Junio C Hamano | 5521883 | 2008-09-07 19:49:25 -0700 | [diff] [blame] | 233 | of the path is kept as long as $H and $M are the same. |
Junio C Hamano | c859600 | 2005-06-07 11:36:30 -0700 | [diff] [blame] | 234 | |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 235 | 3-Way Merge |
| 236 | ~~~~~~~~~~~ |
| 237 | Each "index" entry has two bits worth of "stage" state. stage 0 is the |
| 238 | normal one, and is the only one you'd see in any kind of normal use. |
| 239 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 240 | However, when you do 'git read-tree' with three trees, the "stage" |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 241 | starts out at 1. |
| 242 | |
| 243 | This means that you can do |
| 244 | |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 245 | ---------------- |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 246 | $ git read-tree -m <tree1> <tree2> <tree3> |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 247 | ---------------- |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 248 | |
| 249 | and you will end up with an index with all of the <tree1> entries in |
| 250 | "stage1", all of the <tree2> entries in "stage2" and all of the |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 251 | <tree3> entries in "stage3". When performing a merge of another |
| 252 | branch into the current branch, we use the common ancestor tree |
| 253 | as <tree1>, the current branch head as <tree2>, and the other |
| 254 | branch head as <tree3>. |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 255 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 256 | Furthermore, 'git read-tree' has special-case logic that says: if you see |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 257 | a file that matches in all respects in the following states, it |
| 258 | "collapses" back to "stage0": |
| 259 | |
| 260 | - stage 2 and 3 are the same; take one or the other (it makes no |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 261 | difference - the same work has been done on our branch in |
| 262 | stage 2 and their branch in stage 3) |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 263 | |
| 264 | - stage 1 and stage 2 are the same and stage 3 is different; take |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 265 | stage 3 (our branch in stage 2 did not do anything since the |
| 266 | ancestor in stage 1 while their branch in stage 3 worked on |
| 267 | it) |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 268 | |
| 269 | - stage 1 and stage 3 are the same and stage 2 is different take |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 270 | stage 2 (we did something while they did nothing) |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 271 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 272 | The 'git write-tree' command refuses to write a nonsensical tree, and it |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 273 | will complain about unmerged entries if it sees a single entry that is not |
| 274 | stage 0. |
| 275 | |
Horst H. von Brand | abda1ef | 2006-06-03 16:27:26 -0400 | [diff] [blame] | 276 | OK, this all sounds like a collection of totally nonsensical rules, |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 277 | but it's actually exactly what you want in order to do a fast |
| 278 | merge. The different stages represent the "result tree" (stage 0, aka |
| 279 | "merged"), the original tree (stage 1, aka "orig"), and the two trees |
| 280 | you are trying to merge (stage 2 and 3 respectively). |
| 281 | |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 282 | The order of stages 1, 2 and 3 (hence the order of three |
Jason St. John | 06ab60c | 2014-05-21 14:52:26 -0400 | [diff] [blame] | 283 | <tree-ish> command-line arguments) are significant when you |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 284 | start a 3-way merge with an index file that is already |
| 285 | populated. Here is an outline of how the algorithm works: |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 286 | |
| 287 | - if a file exists in identical format in all three trees, it will |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 288 | automatically collapse to "merged" state by 'git read-tree'. |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 289 | |
| 290 | - a file that has _any_ difference what-so-ever in the three trees |
Johannes Schindelin | 2c6e477 | 2005-08-05 17:05:02 +0200 | [diff] [blame] | 291 | will stay as separate entries in the index. It's up to "porcelain |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 292 | policy" to determine how to remove the non-0 stages, and insert a |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 293 | merged version. |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 294 | |
| 295 | - the index file saves and restores with all this information, so you |
| 296 | can merge things incrementally, but as long as it has entries in |
Horst H. von Brand | abda1ef | 2006-06-03 16:27:26 -0400 | [diff] [blame] | 297 | stages 1/2/3 (i.e., "unmerged entries") you can't write the result. So |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 298 | now the merge algorithm ends up being really simple: |
| 299 | |
| 300 | * you walk the index in order, and ignore all entries of stage 0, |
| 301 | since they've already been done. |
| 302 | |
| 303 | * if you find a "stage1", but no matching "stage2" or "stage3", you |
| 304 | know it's been removed from both trees (it only existed in the |
| 305 | original tree), and you remove that entry. |
| 306 | |
| 307 | * if you find a matching "stage2" and "stage3" tree, you remove one |
| 308 | of them, and turn the other into a "stage0" entry. Remove any |
| 309 | matching "stage1" entry if it exists too. .. all the normal |
| 310 | trivial rules .. |
| 311 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 312 | You would normally use 'git merge-index' with supplied |
| 313 | 'git merge-one-file' to do this last step. The script updates |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 314 | the files in the working tree as it merges each path and at the |
| 315 | end of a successful merge. |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 316 | |
| 317 | When you start a 3-way merge with an index file that is already |
| 318 | populated, it is assumed that it represents the state of the |
| 319 | files in your work tree, and you can even have files with |
| 320 | changes unrecorded in the index file. It is further assumed |
| 321 | that this state is "derived" from the stage 2 tree. The 3-way |
| 322 | merge refuses to run if it finds an entry in the original index |
| 323 | file that does not match stage 2. |
| 324 | |
| 325 | This is done to prevent you from losing your work-in-progress |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 326 | changes, and mixing your random changes in an unrelated merge |
| 327 | commit. To illustrate, suppose you start from what has been |
Francis Daly | 3742506 | 2006-06-07 13:56:45 +0100 | [diff] [blame] | 328 | committed last to your repository: |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 329 | |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 330 | ---------------- |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 331 | $ JC=`git rev-parse --verify "HEAD^0"` |
| 332 | $ git checkout-index -f -u -a $JC |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 333 | ---------------- |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 334 | |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 335 | You do random edits, without running 'git update-index'. And then |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 336 | you notice that the tip of your "upstream" tree has advanced |
| 337 | since you pulled from him: |
| 338 | |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 339 | ---------------- |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 340 | $ git fetch git://.... linus |
Joey Hess | 96890f4 | 2011-12-26 12:16:56 -0400 | [diff] [blame] | 341 | $ LT=`git rev-parse FETCH_HEAD` |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 342 | ---------------- |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 343 | |
| 344 | Your work tree is still based on your HEAD ($JC), but you have |
| 345 | some edits since. Three-way merge makes sure that you have not |
Lukas_Sandström | 5f3aa19 | 2005-11-11 02:12:27 +0100 | [diff] [blame] | 346 | added or modified index entries since $JC, and if you haven't, |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 347 | then does the right thing. So with the following sequence: |
| 348 | |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 349 | ---------------- |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 350 | $ git read-tree -m -u `git merge-base $JC $LT` $JC $LT |
| 351 | $ git merge-index git-merge-one-file -a |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 352 | $ echo "Merge with Linus" | \ |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 353 | git commit-tree `git write-tree` -p $JC -p $LT |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 354 | ---------------- |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 355 | |
Jon Loeliger | 61f693b | 2005-12-05 23:13:03 -0600 | [diff] [blame] | 356 | what you would commit is a pure merge between $JC and $LT without |
Junio C Hamano | ccef66b | 2005-06-07 14:35:43 -0700 | [diff] [blame] | 357 | your work-in-progress changes, and your work tree would be |
| 358 | updated to the result of the merge. |
| 359 | |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 360 | However, if you have local changes in the working tree that |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 361 | would be overwritten by this merge, 'git read-tree' will refuse |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 362 | to run to prevent your changes from being lost. |
| 363 | |
| 364 | In other words, there is no need to worry about what exists only |
| 365 | in the working tree. When you have local changes in a part of |
| 366 | the project that is not involved in the merge, your changes do |
| 367 | not interfere with the merge, and are kept intact. When they |
Thomas Rast | 0b444cd | 2010-01-10 00:33:00 +0100 | [diff] [blame] | 368 | *do* interfere, the merge does not even start ('git read-tree' |
Junio C Hamano | bb6d7b8 | 2005-12-05 23:26:10 -0800 | [diff] [blame] | 369 | complains loudly and fails without modifying anything). In such |
| 370 | a case, you can simply continue doing what you were in the |
| 371 | middle of doing, and when your working tree is ready (i.e. you |
| 372 | have finished your work-in-progress), attempt the merge again. |
| 373 | |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 374 | |
Nguyễn Thái Ngọc Duy | 76a8788 | 2018-04-30 17:35:33 +0200 | [diff] [blame] | 375 | SPARSE CHECKOUT |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 376 | --------------- |
| 377 | |
Elijah Newren | 5d4b293 | 2022-04-22 02:32:26 +0000 | [diff] [blame] | 378 | Note: The skip-worktree capabilities in linkgit:git-update-index[1] |
| 379 | and `read-tree` predated the introduction of |
| 380 | linkgit:git-sparse-checkout[1]. Users are encouraged to use the |
| 381 | `sparse-checkout` command in preference to these plumbing commands for |
| 382 | sparse-checkout/skip-worktree related needs. However, the information |
| 383 | below might be useful to users trying to understand the pattern style |
| 384 | used in non-cone mode of the `sparse-checkout` command. |
Elijah Newren | 9023535 | 2022-01-14 15:59:42 +0000 | [diff] [blame] | 385 | |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 386 | "Sparse checkout" allows populating the working directory sparsely. |
Elijah Newren | 9023535 | 2022-01-14 15:59:42 +0000 | [diff] [blame] | 387 | It uses the skip-worktree bit (see linkgit:git-update-index[1]) to |
| 388 | tell Git whether a file in the working directory is worth looking at. |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 389 | |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 390 | 'git read-tree' and other merge-based commands ('git merge', 'git |
| 391 | checkout'...) can help maintaining the skip-worktree bitmap and working |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 392 | directory update. `$GIT_DIR/info/sparse-checkout` is used to |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 393 | define the skip-worktree reference bitmap. When 'git read-tree' needs |
| 394 | to update the working directory, it resets the skip-worktree bit in the index |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 395 | based on this file, which uses the same syntax as .gitignore files. |
Elijah Newren | 9023535 | 2022-01-14 15:59:42 +0000 | [diff] [blame] | 396 | If an entry matches a pattern in this file, or the entry corresponds to |
| 397 | a file present in the working tree, then skip-worktree will not be |
Michael J Gruber | 1f1f575 | 2011-09-21 09:48:38 +0200 | [diff] [blame] | 398 | set on that entry. Otherwise, skip-worktree will be set. |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 399 | |
| 400 | Then it compares the new skip-worktree value with the previous one. If |
Michael J Gruber | 1f1f575 | 2011-09-21 09:48:38 +0200 | [diff] [blame] | 401 | skip-worktree turns from set to unset, it will add the corresponding |
| 402 | file back. If it turns from unset to set, that file will be removed. |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 403 | |
| 404 | While `$GIT_DIR/info/sparse-checkout` is usually used to specify what |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 405 | files are in, you can also specify what files are _not_ in, using |
| 406 | negate patterns. For example, to remove the file `unwanted`: |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 407 | |
| 408 | ---------------- |
Nguyễn Thái Ngọc Duy | 5e82123 | 2011-09-26 09:09:15 +1000 | [diff] [blame] | 409 | /* |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 410 | !unwanted |
| 411 | ---------------- |
| 412 | |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 413 | Another tricky thing is fully repopulating the working directory when you |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 414 | no longer want sparse checkout. You cannot just disable "sparse |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 415 | checkout" because skip-worktree bits are still in the index and your working |
| 416 | directory is still sparsely populated. You should re-populate the working |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 417 | directory with the `$GIT_DIR/info/sparse-checkout` file content as |
| 418 | follows: |
| 419 | |
| 420 | ---------------- |
Nguyễn Thái Ngọc Duy | 5e82123 | 2011-09-26 09:09:15 +1000 | [diff] [blame] | 421 | /* |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 422 | ---------------- |
| 423 | |
Michael J Gruber | cc1a2b6 | 2011-09-21 09:48:37 +0200 | [diff] [blame] | 424 | Then you can disable sparse checkout. Sparse checkout support in 'git |
| 425 | read-tree' and similar commands is disabled by default. You need to |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 426 | turn `core.sparseCheckout` on in order to have sparse checkout |
| 427 | support. |
Nguyễn Thái Ngọc Duy | ed5336a | 2009-08-20 20:47:05 +0700 | [diff] [blame] | 428 | |
| 429 | |
Junio C Hamano | 56ae8df | 2008-05-28 16:55:27 -0700 | [diff] [blame] | 430 | SEE ALSO |
David Greaves | c1bdacf | 2005-05-10 22:32:38 +0100 | [diff] [blame] | 431 | -------- |
Nihal Jere | 63a3601 | 2022-03-03 10:15:43 -0600 | [diff] [blame] | 432 | linkgit:git-write-tree[1], linkgit:git-ls-files[1], |
| 433 | linkgit:gitignore[5], linkgit:git-sparse-checkout[1] |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 434 | |
David Greaves | 2cf565c | 2005-05-10 22:32:30 +0100 | [diff] [blame] | 435 | GIT |
| 436 | --- |
Christian Couder | 9e1f0a8 | 2008-06-06 09:07:32 +0200 | [diff] [blame] | 437 | Part of the linkgit:git[1] suite |