Code

6412805f06cea538a7d32c1eb8284b045b919b8a
[git.git] / Documentation / git-merge.txt
1 git-merge(1)
2 ============
4 NAME
5 ----
6 git-merge - Join two or more development histories together
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>...
16 DESCRIPTION
17 -----------
18 Incorporates changes from the named commits (since the time their
19 histories diverged from the current branch) into the current
20 branch.  This command is used by 'git pull' to incorporate changes
21 from another repository and can be used by hand to merge changes
22 from one branch into another.
24 Assume the following history exists and the current branch is
25 "`master`":
27 ------------
28           A---B---C topic
29          /
30     D---E---F---G master
31 ------------
33 Then "`git merge topic`" will replay the changes made on the
34 `topic` branch since it diverged from `master` (i.e., `E`) until
35 its current commit (`C`) on top of `master`, and record the result
36 in a new commit along with the names of the two parent commits and
37 a log message from the user describing the changes.
39 ------------
40           A---B---C topic
41          /         \
42     D---E---F---G---H master
43 ------------
45 The second syntax (<msg> `HEAD` <commit>...) is supported for
46 historical reasons.  Do not use it from the command line or in
47 new scripts.  It is the same as `git merge -m <msg> <commit>...`.
49 *Warning*: Running 'git merge' with uncommitted changes is
50 discouraged: while possible, it leaves you in a state that is hard to
51 back out of in the case of a conflict.
54 OPTIONS
55 -------
56 include::merge-options.txt[]
58 -m <msg>::
59         Set the commit message to be used for the merge commit (in
60         case one is created). The 'git fmt-merge-msg' command can be
61         used to give a good default for automated 'git merge'
62         invocations.
64 <commit>...::
65         Commits, usually other branch heads, to merge into our branch.
66         You need at least one <commit>.  Specifying more than one
67         <commit> obviously means you are trying an Octopus.
70 PRE-MERGE CHECKS
71 ----------------
73 Before applying outside changes, you should get your own work in
74 good shape and committed locally, so it will not be clobbered if
75 there are conflicts.  See also linkgit:git-stash[1].
76 'git pull' and 'git merge' will stop without doing anything when
77 local uncommitted changes overlap with files that 'git pull'/'git
78 merge' may need to update.
80 To avoid recording unrelated changes in the merge commit,
81 'git pull' and 'git merge' will also abort if there are any changes
82 registered in the index relative to the `HEAD` commit.  (One
83 exception is when the changed index entries are in the state that
84 would result from the merge already.)
86 If all named commits are already ancestors of `HEAD`, 'git merge'
87 will exit early with the message "Already up-to-date."
89 FAST-FORWARD MERGE
90 ------------------
92 Often the current branch head is an ancestor of the named commit.
93 This is the most common case especially when invoked from 'git
94 pull': you are tracking an upstream repository, you have committed
95 no local changes, and now you want to update to a newer upstream
96 revision.  In this case, a new commit is not needed to store the
97 combined history; instead, the `HEAD` (along with the index) is
98 updated to point at the named commit, without creating an extra
99 merge commit.
101 This behavior can be suppressed with the `--no-ff` option.
103 TRUE MERGE
104 ----------
106 Except in a fast-forward merge (see above), the branches to be
107 merged must be tied together by a merge commit that has both of them
108 as its parents.
110 A merged version reconciling the changes from all branches to be
111 merged is committed, and your `HEAD`, index, and working tree are
112 updated to it.  It is possible to have modifications in the working
113 tree as long as they do not overlap; the update will preserve them.
115 When it is not obvious how to reconcile the changes, the following
116 happens:
118 1. The `HEAD` pointer stays the same.
119 2. The `MERGE_HEAD` ref is set to point to the other branch head.
120 3. Paths that merged cleanly are updated both in the index file and
121    in your working tree.
122 4. For conflicting paths, the index file records up to three
123    versions: stage 1 stores the version from the common ancestor,
124    stage 2 from `HEAD`, and stage 3 from `MERGE_HEAD` (you
125    can inspect the stages with `git ls-files -u`).  The working
126    tree files contain the result of the "merge" program; i.e. 3-way
127    merge results with familiar conflict markers `<<<` `===` `>>>`.
128 5. No other changes are made.  In particular, the local
129    modifications you had before you started merge will stay the
130    same and the index entries for them stay as they were,
131    i.e. matching `HEAD`.
133 If you tried a merge which resulted in complex conflicts and
134 want to start over, you can recover with `git reset --merge`.
136 HOW CONFLICTS ARE PRESENTED
137 ---------------------------
139 During a merge, the working tree files are updated to reflect the result
140 of the merge.  Among the changes made to the common ancestor's version,
141 non-overlapping ones (that is, you changed an area of the file while the
142 other side left that area intact, or vice versa) are incorporated in the
143 final result verbatim.  When both sides made changes to the same area,
144 however, git cannot randomly pick one side over the other, and asks you to
145 resolve it by leaving what both sides did to that area.
147 By default, git uses the same style as that is used by "merge" program
148 from the RCS suite to present such a conflicted hunk, like this:
150 ------------
151 Here are lines that are either unchanged from the common
152 ancestor, or cleanly resolved because only one side changed.
153 <<<<<<< yours:sample.txt
154 Conflict resolution is hard;
155 let's go shopping.
156 =======
157 Git makes conflict resolution easy.
158 >>>>>>> theirs:sample.txt
159 And here is another line that is cleanly resolved or unmodified.
160 ------------
162 The area where a pair of conflicting changes happened is marked with markers
163 `<<<<<<<`, `=======`, and `>>>>>>>`.  The part before the `=======`
164 is typically your side, and the part afterwards is typically their side.
166 The default format does not show what the original said in the conflicting
167 area.  You cannot tell how many lines are deleted and replaced with
168 Barbie's remark on your side.  The only thing you can tell is that your
169 side wants to say it is hard and you'd prefer to go shopping, while the
170 other side wants to claim it is easy.
172 An alternative style can be used by setting the "merge.conflictstyle"
173 configuration variable to "diff3".  In "diff3" style, the above conflict
174 may look like this:
176 ------------
177 Here are lines that are either unchanged from the common
178 ancestor, or cleanly resolved because only one side changed.
179 <<<<<<< yours:sample.txt
180 Conflict resolution is hard;
181 let's go shopping.
182 |||||||
183 Conflict resolution is hard.
184 =======
185 Git makes conflict resolution easy.
186 >>>>>>> theirs:sample.txt
187 And here is another line that is cleanly resolved or unmodified.
188 ------------
190 In addition to the `<<<<<<<`, `=======`, and `>>>>>>>` markers, it uses
191 another `|||||||` marker that is followed by the original text.  You can
192 tell that the original just stated a fact, and your side simply gave in to
193 that statement and gave up, while the other side tried to have a more
194 positive attitude.  You can sometimes come up with a better resolution by
195 viewing the original.
198 HOW TO RESOLVE CONFLICTS
199 ------------------------
201 After seeing a conflict, you can do two things:
203  * Decide not to merge.  The only clean-ups you need are to reset
204    the index file to the `HEAD` commit to reverse 2. and to clean
205    up working tree changes made by 2. and 3.; `git-reset --hard` can
206    be used for this.
208  * Resolve the conflicts.  Git will mark the conflicts in
209    the working tree.  Edit the files into shape and
210    'git add' them to the index.  Use 'git commit' to seal the deal.
212 You can work through the conflict with a number of tools:
214  * Use a mergetool.  `git mergetool` to launch a graphical
215    mergetool which will work you through the merge.
217  * Look at the diffs.  `git diff` will show a three-way diff,
218    highlighting changes from both the HEAD and their versions.
220  * Look at the diffs on their own. `git log --merge -p <path>`
221    will show diffs first for the HEAD version and then
222    their version.
224  * Look at the originals.  `git show :1:filename` shows the
225    common ancestor, `git show :2:filename` shows the HEAD
226    version and `git show :3:filename` shows their version.
229 EXAMPLES
230 --------
232 * Merge branches `fixes` and `enhancements` on top of
233   the current branch, making an octopus merge:
235 ------------------------------------------------
236 $ git merge fixes enhancements
237 ------------------------------------------------
239 * Merge branch `obsolete` into the current branch, using `ours`
240   merge strategy:
242 ------------------------------------------------
243 $ git merge -s ours obsolete
244 ------------------------------------------------
246 * Merge branch `maint` into the current branch, but do not make
247   a new commit automatically:
249 ------------------------------------------------
250 $ git merge --no-commit maint
251 ------------------------------------------------
253 This can be used when you want to include further changes to the
254 merge, or want to write your own merge commit message.
256 You should refrain from abusing this option to sneak substantial
257 changes into a merge commit.  Small fixups like bumping
258 release/version name would be acceptable.
261 include::merge-strategies.txt[]
263 CONFIGURATION
264 -------------
265 include::merge-config.txt[]
267 branch.<name>.mergeoptions::
268         Sets default options for merging into branch <name>. The syntax and
269         supported options are the same as those of 'git merge', but option
270         values containing whitespace characters are currently not supported.
272 SEE ALSO
273 --------
274 linkgit:git-fmt-merge-msg[1], linkgit:git-pull[1],
275 linkgit:gitattributes[5],
276 linkgit:git-reset[1],
277 linkgit:git-diff[1], linkgit:git-ls-files[1],
278 linkgit:git-add[1], linkgit:git-rm[1],
279 linkgit:git-mergetool[1]
281 Author
282 ------
283 Written by Junio C Hamano <gitster@pobox.com>
286 Documentation
287 --------------
288 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
290 GIT
291 ---
292 Part of the linkgit:git[1] suite