Documentation: suggest `reset --merge` in How Merge Works section
[git] / Documentation / git-merge.txt
1 git-merge(1)
2 ============
3
4 NAME
5 ----
6 git-merge - Join two or more development histories together
7
8
9 SYNOPSIS
10 --------
11 [verse]
12 'git merge' [-n] [--stat] [--no-commit] [--squash] [-s <strategy>]...
13         [-m <msg>] <commit>...
14 'git merge' <msg> HEAD <commit>...
15
16 DESCRIPTION
17 -----------
18 Merges the history specified by <commit> into HEAD, optionally using a
19 specific merge strategy.
20
21 The second syntax (<msg> `HEAD` <commit>...) is supported for
22 historical reasons.  Do not use it from the command line or in
23 new scripts.  It is the same as `git merge -m <msg> <commit>...`.
24
25 *Warning*: Running 'git merge' with uncommitted changes is
26 discouraged: while possible, it leaves you in a state that is hard to
27 back out of in the case of a conflict.
28
29
30 OPTIONS
31 -------
32 include::merge-options.txt[]
33
34 -m <msg>::
35         Set the commit message to be used for the merge commit (in
36         case one is created). The 'git fmt-merge-msg' command can be
37         used to give a good default for automated 'git merge'
38         invocations.
39
40 <commit>...::
41         Commits, usually other branch heads, to merge into our branch.
42         You need at least one <commit>.  Specifying more than one
43         <commit> obviously means you are trying an Octopus.
44
45 include::merge-strategies.txt[]
46
47
48 HOW MERGE WORKS
49 ---------------
50
51 A merge is always between the current `HEAD` and one or more
52 commits (usually, branch head or tag), and the index file must
53 match the tree of `HEAD` commit (i.e. the contents of the last commit)
54 when it starts out.  In other words, `git diff --cached HEAD` must
55 report no changes.  (One exception is when the changed index
56 entries are already in the same state that would result from
57 the merge anyway.)
58
59 Three kinds of merge can happen:
60
61 * The merged commit is already contained in `HEAD`. This is the
62   simplest case, called "Already up-to-date."
63
64 * `HEAD` is already contained in the merged commit. This is the
65   most common case especially when invoked from 'git pull':
66   you are tracking an upstream repository, have committed no local
67   changes and now you want to update to a newer upstream revision.
68   Your `HEAD` (and the index) is updated to point at the merged
69   commit, without creating an extra merge commit.  This is
70   called "Fast-forward".
71
72 * Both the merged commit and `HEAD` are independent and must be
73   tied together by a merge commit that has both of them as its parents.
74   The rest of this section describes this "True merge" case.
75
76 The chosen merge strategy merges the two commits into a single
77 new source tree.
78 When things merge cleanly, this is what happens:
79
80 1. The results are updated both in the index file and in your
81    working tree;
82 2. Index file is written out as a tree;
83 3. The tree gets committed; and
84 4. The `HEAD` pointer gets advanced.
85
86 Because of 2., we require that the original state of the index
87 file matches exactly the current `HEAD` commit; otherwise we
88 will write out your local changes already registered in your
89 index file along with the merge result, which is not good.
90 Because 1. involves only those paths differing between your
91 branch and the branch you are merging
92 (which is typically a fraction of the whole tree), you can
93 have local modifications in your working tree as long as they do
94 not overlap with what the merge updates.
95
96 When there are conflicts, the following happens:
97
98 1. `HEAD` stays the same.
99
100 2. Cleanly merged paths are updated both in the index file and
101    in your working tree.
102
103 3. For conflicting paths, the index file records up to three
104    versions; stage1 stores the version from the common ancestor,
105    stage2 from `HEAD`, and stage3 from the other branch (you
106    can inspect the stages with `git ls-files -u`).  The working
107    tree files contain the result of the "merge" program; i.e. 3-way
108    merge results with familiar conflict markers `<<< === >>>`.
109
110 4. No other changes are done.  In particular, the local
111    modifications you had before you started merge will stay the
112    same and the index entries for them stay as they were,
113    i.e. matching `HEAD`.
114
115 If you tried a merge which resulted in complex conflicts and
116 want to start over, you can recover with `git reset --merge`.
117
118 HOW CONFLICTS ARE PRESENTED
119 ---------------------------
120
121 During a merge, the working tree files are updated to reflect the result
122 of the merge.  Among the changes made to the common ancestor's version,
123 non-overlapping ones (that is, you changed an area of the file while the
124 other side left that area intact, or vice versa) are incorporated in the
125 final result verbatim.  When both sides made changes to the same area,
126 however, git cannot randomly pick one side over the other, and asks you to
127 resolve it by leaving what both sides did to that area.
128
129 By default, git uses the same style as that is used by "merge" program
130 from the RCS suite to present such a conflicted hunk, like this:
131
132 ------------
133 Here are lines that are either unchanged from the common
134 ancestor, or cleanly resolved because only one side changed.
135 <<<<<<< yours:sample.txt
136 Conflict resolution is hard;
137 let's go shopping.
138 =======
139 Git makes conflict resolution easy.
140 >>>>>>> theirs:sample.txt
141 And here is another line that is cleanly resolved or unmodified.
142 ------------
143
144 The area where a pair of conflicting changes happened is marked with markers
145 `<<<<<<<`, `=======`, and `>>>>>>>`.  The part before the `=======`
146 is typically your side, and the part afterwards is typically their side.
147
148 The default format does not show what the original said in the conflicting
149 area.  You cannot tell how many lines are deleted and replaced with
150 Barbie's remark on your side.  The only thing you can tell is that your
151 side wants to say it is hard and you'd prefer to go shopping, while the
152 other side wants to claim it is easy.
153
154 An alternative style can be used by setting the "merge.conflictstyle"
155 configuration variable to "diff3".  In "diff3" style, the above conflict
156 may look like this:
157
158 ------------
159 Here are lines that are either unchanged from the common
160 ancestor, or cleanly resolved because only one side changed.
161 <<<<<<< yours:sample.txt
162 Conflict resolution is hard;
163 let's go shopping.
164 |||||||
165 Conflict resolution is hard.
166 =======
167 Git makes conflict resolution easy.
168 >>>>>>> theirs:sample.txt
169 And here is another line that is cleanly resolved or unmodified.
170 ------------
171
172 In addition to the `<<<<<<<`, `=======`, and `>>>>>>>` markers, it uses
173 another `|||||||` marker that is followed by the original text.  You can
174 tell that the original just stated a fact, and your side simply gave in to
175 that statement and gave up, while the other side tried to have a more
176 positive attitude.  You can sometimes come up with a better resolution by
177 viewing the original.
178
179
180 HOW TO RESOLVE CONFLICTS
181 ------------------------
182
183 After seeing a conflict, you can do two things:
184
185  * Decide not to merge.  The only clean-ups you need are to reset
186    the index file to the `HEAD` commit to reverse 2. and to clean
187    up working tree changes made by 2. and 3.; `git-reset --hard` can
188    be used for this.
189
190  * Resolve the conflicts.  Git will mark the conflicts in
191    the working tree.  Edit the files into shape and
192    'git add' them to the index.  Use 'git commit' to seal the deal.
193
194 You can work through the conflict with a number of tools:
195
196  * Use a mergetool.  `git mergetool` to launch a graphical
197    mergetool which will work you through the merge.
198
199  * Look at the diffs.  `git diff` will show a three-way diff,
200    highlighting changes from both the HEAD and their versions.
201
202  * Look at the diffs on their own. `git log --merge -p <path>`
203    will show diffs first for the HEAD version and then
204    their version.
205
206  * Look at the originals.  `git show :1:filename` shows the
207    common ancestor, `git show :2:filename` shows the HEAD
208    version and `git show :3:filename` shows their version.
209
210
211 EXAMPLES
212 --------
213
214 * Merge branches `fixes` and `enhancements` on top of
215   the current branch, making an octopus merge:
216 +
217 ------------------------------------------------
218 $ git merge fixes enhancements
219 ------------------------------------------------
220
221 * Merge branch `obsolete` into the current branch, using `ours`
222   merge strategy:
223 +
224 ------------------------------------------------
225 $ git merge -s ours obsolete
226 ------------------------------------------------
227
228 * Merge branch `maint` into the current branch, but do not make
229   a new commit automatically:
230 +
231 ------------------------------------------------
232 $ git merge --no-commit maint
233 ------------------------------------------------
234 +
235 This can be used when you want to include further changes to the
236 merge, or want to write your own merge commit message.
237 +
238 You should refrain from abusing this option to sneak substantial
239 changes into a merge commit.  Small fixups like bumping
240 release/version name would be acceptable.
241
242
243 CONFIGURATION
244 -------------
245 include::merge-config.txt[]
246
247 branch.<name>.mergeoptions::
248         Sets default options for merging into branch <name>. The syntax and
249         supported options are the same as those of 'git merge', but option
250         values containing whitespace characters are currently not supported.
251
252 SEE ALSO
253 --------
254 linkgit:git-fmt-merge-msg[1], linkgit:git-pull[1],
255 linkgit:gitattributes[5],
256 linkgit:git-reset[1],
257 linkgit:git-diff[1], linkgit:git-ls-files[1],
258 linkgit:git-add[1], linkgit:git-rm[1],
259 linkgit:git-mergetool[1]
260
261 Author
262 ------
263 Written by Junio C Hamano <gitster@pobox.com>
264
265
266 Documentation
267 --------------
268 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
269
270 GIT
271 ---
272 Part of the linkgit:git[1] suite