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