1GIT-MERGE-TREE(1) Git Manual GIT-MERGE-TREE(1)
2
3
4
6 git-merge-tree - Perform merge without touching index or working tree
7
9 git merge-tree [--write-tree] [<options>] <branch1> <branch2>
10 git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2> (deprecated)
11
13 This command has a modern --write-tree mode and a deprecated
14 --trivial-merge mode. With the exception of the DEPRECATED DESCRIPTION
15 section at the end, the rest of this documentation describes modern
16 --write-tree mode.
17
18 Performs a merge, but does not make any new commits and does not read
19 from or write to either the working tree or index.
20
21 The performed merge will use the same feature as the "real" git-
22 merge(1), including:
23
24 • three way content merges of individual files
25
26 • rename detection
27
28 • proper directory/file conflict handling
29
30 • recursive ancestor consolidation (i.e. when there is more than one
31 merge base, creating a virtual merge base by merging the merge
32 bases)
33
34 • etc.
35
36 After the merge completes, a new toplevel tree object is created. See
37 OUTPUT below for details.
38
40 -z
41 Do not quote filenames in the <Conflicted file info> section, and
42 end each filename with a NUL character rather than newline. Also
43 begin the messages section with a NUL character instead of a
44 newline. See the section called “OUTPUT” below for more
45 information.
46
47 --name-only
48 In the Conflicted file info section, instead of writing a list of
49 (mode, oid, stage, path) tuples to output for conflicted files,
50 just provide a list of filenames with conflicts (and do not list
51 filenames multiple times if they have multiple conflicting stages).
52
53 --[no-]messages
54 Write any informational messages such as "Auto-merging <path>" or
55 CONFLICT notices to the end of stdout. If unspecified, the default
56 is to include these messages if there are merge conflicts, and to
57 omit them otherwise.
58
59 --allow-unrelated-histories
60 merge-tree will by default error out if the two branches specified
61 share no common history. This flag can be given to override that
62 check and make the merge proceed anyway.
63
65 For a successful merge, the output from git-merge-tree is simply one
66 line:
67
68 <OID of toplevel tree>
69
70 Whereas for a conflicted merge, the output is by default of the form:
71
72 <OID of toplevel tree>
73 <Conflicted file info>
74 <Informational messages>
75
76 These are discussed individually below.
77
78 However, there is an exception. If --stdin is passed, then there is an
79 extra section at the beginning, a NUL character at the end, and then
80 all the sections repeat for each line of input. Thus, if the first
81 merge is conflicted and the second is clean, the output would be of the
82 form:
83
84 <Merge status>
85 <OID of toplevel tree>
86 <Conflicted file info>
87 <Informational messages>
88 NUL
89 <Merge status>
90 <OID of toplevel tree>
91 NUL
92
93 Merge status
94 This is an integer status followed by a NUL character. The integer
95 status is:
96
97 0: merge had conflicts
98 1: merge was clean
99 <0: something prevented the merge from running (e.g. access to repository
100 objects denied by filesystem)
101
102 OID of toplevel tree
103 This is a tree object that represents what would be checked out in the
104 working tree at the end of git merge. If there were conflicts, then
105 files within this tree may have embedded conflict markers. This section
106 is always followed by a newline (or NUL if -z is passed).
107
108 Conflicted file info
109 This is a sequence of lines with the format
110
111 <mode> <object> <stage> <filename>
112
113 The filename will be quoted as explained for the configuration variable
114 core.quotePath (see git-config(1)). However, if the --name-only option
115 is passed, the mode, object, and stage will be omitted. If -z is
116 passed, the "lines" are terminated by a NUL character instead of a
117 newline character.
118
119 Informational messages
120 This section provides informational messages, typically about
121 conflicts. The format of the section varies significantly depending on
122 whether -z is passed.
123
124 If -z is passed:
125
126 The output format is zero or more conflict informational records, each
127 of the form:
128
129 <list-of-paths><conflict-type>NUL<conflict-message>NUL
130
131 where <list-of-paths> is of the form
132
133 <number-of-paths>NUL<path1>NUL<path2>NUL...<pathN>NUL
134
135 and includes paths (or branch names) affected by the conflict or
136 informational message in <conflict-message>. Also, <conflict-type> is a
137 stable string explaining the type of conflict, such as
138
139 • "Auto-merging"
140
141 • "CONFLICT (rename/delete)"
142
143 • "CONFLICT (submodule lacks merge base)"
144
145 • "CONFLICT (binary)"
146
147 and <conflict-message> is a more detailed message about the conflict
148 which often (but not always) embeds the <stable-short-type-description>
149 within it. These strings may change in future Git versions. Some
150 examples:
151
152 • "Auto-merging <file>"
153
154 • "CONFLICT (rename/delete): <oldfile> renamed...but deleted in..."
155
156 • "Failed to merge submodule <submodule> (no merge base)"
157
158 • "Warning: cannot merge binary files: <filename>"
159
160 If -z is NOT passed:
161
162 This section starts with a blank line to separate it from the previous
163 sections, and then only contains the <conflict-message> information
164 from the previous section (separated by newlines). These are non-stable
165 strings that should not be parsed by scripts, and are just meant for
166 human consumption. Also, note that while <conflict-message> strings
167 usually do not contain embedded newlines, they sometimes do. (However,
168 the free-form messages will never have an embedded NUL character). So,
169 the entire block of information is meant for human readers as an
170 agglomeration of all conflict messages.
171
173 For a successful, non-conflicted merge, the exit status is 0. When the
174 merge has conflicts, the exit status is 1. If the merge is not able to
175 complete (or start) due to some kind of error, the exit status is
176 something other than 0 or 1 (and the output is unspecified). When
177 --stdin is passed, the return status is 0 for both successful and
178 conflicted merges, and something other than 0 or 1 if it cannot
179 complete all the requested merges.
180
182 This command is intended as low-level plumbing, similar to git-hash-
183 object(1), git-mktree(1), git-commit-tree(1), git-write-tree(1), git-
184 update-ref(1), and git-mktag(1). Thus, it can be used as a part of a
185 series of steps such as:
186
187 NEWTREE=$(git merge-tree --write-tree $BRANCH1 $BRANCH2)
188 test $? -eq 0 || die "There were conflicts..."
189 NEWCOMMIT=$(git commit-tree $NEWTREE -p $BRANCH1 -p $BRANCH2)
190 git update-ref $BRANCH1 $NEWCOMMIT
191
192 Note that when the exit status is non-zero, NEWTREE in this sequence
193 will contain a lot more output than just a tree.
194
195 For conflicts, the output includes the same information that you’d get
196 with git-merge(1):
197
198 • what would be written to the working tree (the OID of toplevel
199 tree)
200
201 • the higher order stages that would be written to the index (the
202 Conflicted file info)
203
204 • any messages that would have been printed to stdout (the
205 Informational messages)
206
208 Do NOT look through the resulting toplevel tree to try to find which
209 files conflict; parse the Conflicted file info section instead. Not
210 only would parsing an entire tree be horrendously slow in large
211 repositories, there are numerous types of conflicts not representable
212 by conflict markers (modify/delete, mode conflict, binary file changed
213 on both sides, file/directory conflicts, various rename conflict
214 permutations, etc.)
215
216 Do NOT interpret an empty Conflicted file info list as a clean merge;
217 check the exit status. A merge can have conflicts without having
218 individual files conflict (there are a few types of directory rename
219 conflicts that fall into this category, and others might also be added
220 in the future).
221
222 Do NOT attempt to guess or make the user guess the conflict types from
223 the Conflicted file info list. The information there is insufficient to
224 do so. For example: Rename/rename(1to2) conflicts (both sides renamed
225 the same file differently) will result in three different file having
226 higher order stages (but each only has one higher order stage), with no
227 way (short of the Informational messages section) to determine which
228 three files are related. File/directory conflicts also result in a file
229 with exactly one higher order stage.
230 Possibly-involved-in-directory-rename conflicts (when
231 "merge.directoryRenames" is unset or set to "conflicts") also result in
232 a file with exactly one higher order stage. In all cases, the
233 Informational messages section has the necessary info, though it is not
234 designed to be machine parseable.
235
236 Do NOT assume that each paths from Conflicted file info, and the
237 logical conflicts in the Informational messages have a one-to-one
238 mapping, nor that there is a one-to-many mapping, nor a many-to-one
239 mapping. Many-to-many mappings exist, meaning that each path can have
240 many logical conflict types in a single merge, and each logical
241 conflict type can affect many paths.
242
243 Do NOT assume all filenames listed in the Informational messages
244 section had conflicts. Messages can be included for files that have no
245 conflicts, such as "Auto-merging <file>".
246
247 AVOID taking the OIDS from the Conflicted file info and re-merging them
248 to present the conflicts to the user. This will lose information.
249 Instead, look up the version of the file found within the OID of
250 toplevel tree and show that instead. In particular, the latter will
251 have conflict markers annotated with the original branch/commit being
252 merged and, if renames were involved, the original filename. While you
253 could include the original branch/commit in the conflict marker
254 annotations when re-merging, the original filename is not available
255 from the Conflicted file info and thus you would be losing information
256 that might help the user resolve the conflict.
257
259 Per the DESCRIPTION and unlike the rest of this documentation, this
260 section describes the deprecated --trivial-merge mode.
261
262 Other than the optional --trivial-merge, this mode accepts no options.
263
264 This mode reads three tree-ish, and outputs trivial merge results and
265 conflicting stages to the standard output in a semi-diff format. Since
266 this was designed for higher level scripts to consume and merge the
267 results back into the index, it omits entries that match <branch1>. The
268 result of this second form is similar to what three-way git read-tree
269 -m does, but instead of storing the results in the index, the command
270 outputs the entries to the standard output.
271
272 This form not only has limited applicability (a trivial merge cannot
273 handle content merges of individual files, rename detection, proper
274 directory/file conflict handling, etc.), the output format is also
275 difficult to work with, and it will generally be less performant than
276 the first form even on successful merges (especially if working in
277 large repositories).
278
280 Part of the git(1) suite
281
282
283
284Git 2.39.1 2023-01-13 GIT-MERGE-TREE(1)