Documentation / git-merge.txton commit rerere.autoupdate: change the message when autoupdate is in effect (9196e82)
   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        [-m <msg>] <remote> <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        The commit message to be used for the merge commit (in case
  32        it is created). The 'git-fmt-merge-msg' script can be used
  33        to give a good default for automated 'git-merge' invocations.
  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
  40include::merge-strategies.txt[]
  41
  42
  43If you tried a merge which resulted in a complex conflicts and
  44would want to start over, you can recover with 'git-reset'.
  45
  46CONFIGURATION
  47-------------
  48include::merge-config.txt[]
  49
  50branch.<name>.mergeoptions::
  51        Sets default options for merging into branch <name>. The syntax and
  52        supported options are equal to that of 'git-merge', but option values
  53        containing whitespace characters are currently not supported.
  54
  55HOW MERGE WORKS
  56---------------
  57
  58A merge is always between the current `HEAD` and one or more
  59commits (usually, branch head or tag), and the index file must
  60exactly match the
  61tree of `HEAD` commit (i.e. the contents of the last commit) when
  62it happens.  In other words, `git diff --cached HEAD` must
  63report no changes.
  64
  65[NOTE]
  66This is a bit of a lie.  In certain special cases, your index is
  67allowed to be different from the tree of the `HEAD` commit.  The most
  68notable case is when your `HEAD` commit is already ahead of what
  69is being merged, in which case your index can have arbitrary
  70differences from your `HEAD` commit.  Also, your index entries
  71may have differences from your `HEAD` commit that match
  72the result of a trivial merge (e.g. you received the same patch
  73from an external source to produce the same result as what you are
  74merging).  For example, if a path did not exist in the common
  75ancestor and your head commit but exists in the tree you are
  76merging into your repository, and if you already happen to have
  77that path exactly in your index, the merge does not have to
  78fail.
  79
  80Otherwise, merge will refuse to do any harm to your repository
  81(that is, it may fetch the objects from remote, and it may even
  82update the local branch used to keep track of the remote branch
  83with `git pull remote rbranch:lbranch`, but your working tree,
  84`.git/HEAD` pointer and index file are left intact).  In addition,
  85merge always sets `.git/ORIG_HEAD` to the original state of HEAD so
  86a problematic merge can be removed by using `git reset ORIG_HEAD`.
  87
  88You may have local modifications in the working tree files.  In
  89other words, 'git-diff' is allowed to report changes.
  90However, the merge uses your working tree as the working area,
  91and in order to prevent the merge operation from losing such
  92changes, it makes sure that they do not interfere with the
  93merge. Those complex tables in read-tree documentation define
  94what it means for a path to "interfere with the merge".  And if
  95your local modifications interfere with the merge, again, it
  96stops before touching anything.
  97
  98So in the above two "failed merge" case, you do not have to
  99worry about loss of data --- you simply were not ready to do
 100a merge, so no merge happened at all.  You may want to finish
 101whatever you were in the middle of doing, and retry the same
 102pull after you are done and ready.
 103
 104When things cleanly merge, these things happen:
 105
 1061. The results are updated both in the index file and in your
 107   working tree;
 1082. Index file is written out as a tree;
 1093. The tree gets committed; and
 1104. The `HEAD` pointer gets advanced.
 111
 112Because of 2., we require that the original state of the index
 113file to match exactly the current `HEAD` commit; otherwise we
 114will write out your local changes already registered in your
 115index file along with the merge result, which is not good.
 116Because 1. involves only the paths different between your
 117branch and the remote branch you are pulling from during the
 118merge (which is typically a fraction of the whole tree), you can
 119have local modifications in your working tree as long as they do
 120not overlap with what the merge updates.
 121
 122When there are conflicts, these things happen:
 123
 1241. `HEAD` stays the same.
 125
 1262. Cleanly merged paths are updated both in the index file and
 127   in your working tree.
 128
 1293. For conflicting paths, the index file records up to three
 130   versions; stage1 stores the version from the common ancestor,
 131   stage2 from `HEAD`, and stage3 from the remote branch (you
 132   can inspect the stages with `git ls-files -u`).  The working
 133   tree files have the result of "merge" program; i.e. 3-way
 134   merge result with familiar conflict markers `<<< === >>>`.
 135
 1364. No other changes are done.  In particular, the local
 137   modifications you had before you started merge will stay the
 138   same and the index entries for them stay as they were,
 139   i.e. matching `HEAD`.
 140
 141After seeing a conflict, you can do two things:
 142
 143 * Decide not to merge.  The only clean-up you need are to reset
 144   the index file to the `HEAD` commit to reverse 2. and to clean
 145   up working tree changes made by 2. and 3.; 'git-reset' can
 146   be used for this.
 147
 148 * Resolve the conflicts.  `git diff` would report only the
 149   conflicting paths because of the above 2. and 3.  Edit the
 150   working tree files into a desirable shape, 'git-add' or 'git-rm'
 151   them, to make the index file contain what the merge result
 152   should be, and run 'git-commit' to commit the result.
 153
 154
 155SEE ALSO
 156--------
 157linkgit:git-fmt-merge-msg[1], linkgit:git-pull[1],
 158linkgit:gitattributes[5],
 159linkgit:git-reset[1],
 160linkgit:git-diff[1], linkgit:git-ls-files[1],
 161linkgit:git-add[1], linkgit:git-rm[1],
 162linkgit:git-mergetool[1]
 163
 164Author
 165------
 166Written by Junio C Hamano <junkio@cox.net>
 167
 168
 169Documentation
 170--------------
 171Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
 172
 173GIT
 174---
 175Part of the linkgit:git[1] suite