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