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