Code

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