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