Documentation / git-merge.txton commit Merge branch 'master' into js/shallow (37818d7)
   1git-merge(1)
   2============
   3
   4NAME
   5----
   6git-merge - Grand Unified Merge Driver
   7
   8
   9SYNOPSIS
  10--------
  11[verse]
  12'git-merge' [-n] [--no-commit] [--squash] [-s <strategy>]...
  13        [--reflog-action=<action>]
  14        -m=<msg> <remote> <remote>...
  15
  16DESCRIPTION
  17-----------
  18This is the top-level interface to the merge machinery
  19which drives multiple merge strategy scripts.
  20
  21
  22OPTIONS
  23-------
  24include::merge-options.txt[]
  25
  26<msg>::
  27        The commit message to be used for the merge commit (in case
  28        it is created). The `git-fmt-merge-msg` script can be used
  29        to give a good default for automated `git-merge` invocations.
  30
  31<head>::
  32        Our branch head commit.  This has to be `HEAD`, so new
  33        syntax does not require it
  34
  35<remote>::
  36        Other branch head merged into our branch.  You need at
  37        least one <remote>.  Specifying more than one <remote>
  38        obviously means you are trying an Octopus.
  39
  40--reflog-action=<action>::
  41        This is used internally when `git-pull` calls this command
  42        to record that the merge was created by `pull` command
  43        in the `ref-log` entry that results from the merge.
  44
  45include::merge-strategies.txt[]
  46
  47
  48If you tried a merge which resulted in a complex conflicts and
  49would want to start over, you can recover with
  50gitlink:git-reset[1].
  51
  52
  53HOW MERGE WORKS
  54---------------
  55
  56A merge is always between the current `HEAD` and one or more
  57remote branch heads, and the index file must exactly match the
  58tree of `HEAD` commit (i.e. the contents of the last commit) when
  59it happens.  In other words, `git-diff --cached HEAD` must
  60report no changes.
  61
  62[NOTE]
  63This is a bit of lie.  In certain special cases, your index are
  64allowed to be different from the tree of `HEAD` commit.  The most
  65notable case is when your `HEAD` commit is already ahead of what
  66is being merged, in which case your index can have arbitrary
  67difference from your `HEAD` commit.  Otherwise, your index entries
  68are allowed have differences from your `HEAD` commit that match
  69the result of trivial merge (e.g. you received the same patch
  70from external source to produce the same result as what you are
  71merging).  For example, if a path did not exist in the common
  72ancestor and your head commit but exists in the tree you are
  73merging into your repository, and if you already happen to have
  74that path exactly in your index, the merge does not have to
  75fail.
  76
  77Otherwise, merge will refuse to do any harm to your repository
  78(that is, it may fetch the objects from remote, and it may even
  79update the local branch used to keep track of the remote branch
  80with `git pull remote rbranch:lbranch`, but your working tree,
  81`.git/HEAD` pointer and index file are left intact).
  82
  83You may have local modifications in the working tree files.  In
  84other words, `git-diff` is allowed to report changes.
  85However, the merge uses your working tree as the working area,
  86and in order to prevent the merge operation from losing such
  87changes, it makes sure that they do not interfere with the
  88merge. Those complex tables in read-tree documentation define
  89what it means for a path to "interfere with the merge".  And if
  90your local modifications interfere with the merge, again, it
  91stops before touching anything.
  92
  93So in the above two "failed merge" case, you do not have to
  94worry about loss of data --- you simply were not ready to do
  95a merge, so no merge happened at all.  You may want to finish
  96whatever you were in the middle of doing, and retry the same
  97pull after you are done and ready.
  98
  99When things cleanly merge, these things happen:
 100
 1011. the results are updated both in the index file and in your
 102   working tree,
 1032. index file is written out as a tree,
 1043. the tree gets committed, and 
 1054. the `HEAD` pointer gets advanced.
 106
 107Because of 2., we require that the original state of the index
 108file to match exactly the current `HEAD` commit; otherwise we
 109will write out your local changes already registered in your
 110index file along with the merge result, which is not good.
 111Because 1. involves only the paths different between your
 112branch and the remote branch you are pulling from during the
 113merge (which is typically a fraction of the whole tree), you can
 114have local modifications in your working tree as long as they do
 115not overlap with what the merge updates.
 116
 117When there are conflicts, these things happen:
 118
 1191. `HEAD` stays the same.
 120
 1212. Cleanly merged paths are updated both in the index file and
 122   in your working tree.
 123
 1243. For conflicting paths, the index file records up to three
 125   versions; stage1 stores the version from the common ancestor,
 126   stage2 from `HEAD`, and stage3 from the remote branch (you
 127   can inspect the stages with `git-ls-files -u`).  The working
 128   tree files have the result of "merge" program; i.e. 3-way
 129   merge result with familiar conflict markers `<<< === >>>`.
 130
 1314. No other changes are done.  In particular, the local
 132   modifications you had before you started merge will stay the
 133   same and the index entries for them stay as they were,
 134   i.e. matching `HEAD`.
 135
 136After seeing a conflict, you can do two things:
 137
 138 * Decide not to merge.  The only clean-up you need are to reset
 139   the index file to the `HEAD` commit to reverse 2. and to clean
 140   up working tree changes made by 2. and 3.; `git-reset` can
 141   be used for this.
 142
 143 * Resolve the conflicts.  `git-diff` would report only the
 144   conflicting paths because of the above 2. and 3..  Edit the
 145   working tree files into a desirable shape, `git-update-index`
 146   them, to make the index file contain what the merge result
 147   should be, and run `git-commit` to commit the result.
 148
 149
 150SEE ALSO
 151--------
 152gitlink:git-fmt-merge-msg[1], gitlink:git-pull[1]
 153
 154
 155Author
 156------
 157Written by Junio C Hamano <junkio@cox.net>
 158
 159
 160Documentation
 161--------------
 162Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
 163
 164GIT
 165---
 166Part of the gitlink:git[7] suite