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 [--[no-]rerere-autoupdate] [-m <msg>] <commit>... 14'git merge' <msg> HEAD <commit>... 15 16DESCRIPTION 17----------- 18Incorporates changes from the named commits (since the time their 19histories diverged from the current branch) into the current 20branch. This command is used by 'git pull' to incorporate changes 21from another repository and can be used by hand to merge changes 22from one branch into another. 23 24Assume the following history exists and the current branch is 25"`master`": 26 27------------ 28 A---B---C topic 29 / 30 D---E---F---G master 31------------ 32 33Then "`git merge topic`" will replay the changes made on the 34`topic` branch since it diverged from `master` (i.e., `E`) until 35its current commit (`C`) on top of `master`, and record the result 36in a new commit along with the names of the two parent commits and 37a log message from the user describing the changes. 38 39------------ 40 A---B---C topic 41 / \ 42 D---E---F---G---H master 43------------ 44 45The second syntax (<msg> `HEAD` <commit>...) is supported for 46historical reasons. Do not use it from the command line or in 47new scripts. It is the same as `git merge -m <msg> <commit>...`. 48 49*Warning*: Running 'git merge' with uncommitted changes is 50discouraged: while possible, it leaves you in a state that is hard to 51back out of in the case of a conflict. 52 53 54OPTIONS 55------- 56include::merge-options.txt[] 57 58-m <msg>:: 59 Set the commit message to be used for the merge commit (in 60 case one is created). The 'git fmt-merge-msg' command can be 61 used to give a good default for automated 'git merge' 62 invocations. 63 64--rerere-autoupdate:: 65--no-rerere-autoupdate:: 66 Allow the rerere mechanism to update the index with the 67 result of auto-conflict resolution if possible. 68 69<commit>...:: 70 Commits, usually other branch heads, to merge into our branch. 71 You need at least one <commit>. Specifying more than one 72 <commit> obviously means you are trying an Octopus. 73 74 75PRE-MERGE CHECKS 76---------------- 77 78Before applying outside changes, you should get your own work in 79good shape and committed locally, so it will not be clobbered if 80there are conflicts. See also linkgit:git-stash[1]. 81'git pull' and 'git merge' will stop without doing anything when 82local uncommitted changes overlap with files that 'git pull'/'git 83merge' may need to update. 84 85To avoid recording unrelated changes in the merge commit, 86'git pull' and 'git merge' will also abort if there are any changes 87registered in the index relative to the `HEAD` commit. (One 88exception is when the changed index entries are in the state that 89would result from the merge already.) 90 91If all named commits are already ancestors of `HEAD`, 'git merge' 92will exit early with the message "Already up-to-date." 93 94FAST-FORWARD MERGE 95------------------ 96 97Often the current branch head is an ancestor of the named commit. 98This is the most common case especially when invoked from 'git 99pull': you are tracking an upstream repository, you have committed 100no local changes, and now you want to update to a newer upstream 101revision. In this case, a new commit is not needed to store the 102combined history; instead, the `HEAD` (along with the index) is 103updated to point at the named commit, without creating an extra 104merge commit. 105 106This behavior can be suppressed with the `--no-ff` option. 107 108TRUE MERGE 109---------- 110 111Except in a fast-forward merge (see above), the branches to be 112merged must be tied together by a merge commit that has both of them 113as its parents. 114 115A merged version reconciling the changes from all branches to be 116merged is committed, and your `HEAD`, index, and working tree are 117updated to it. It is possible to have modifications in the working 118tree as long as they do not overlap; the update will preserve them. 119 120When it is not obvious how to reconcile the changes, the following 121happens: 122 1231. The `HEAD` pointer stays the same. 1242. The `MERGE_HEAD` ref is set to point to the other branch head. 1253. Paths that merged cleanly are updated both in the index file and 126 in your working tree. 1274. For conflicting paths, the index file records up to three 128 versions: stage 1 stores the version from the common ancestor, 129 stage 2 from `HEAD`, and stage 3 from `MERGE_HEAD` (you 130 can inspect the stages with `git ls-files -u`). The working 131 tree files contain the result of the "merge" program; i.e. 3-way 132 merge results with familiar conflict markers `<<<` `===` `>>>`. 1335. No other changes are made. In particular, the local 134 modifications you had before you started merge will stay the 135 same and the index entries for them stay as they were, 136 i.e. matching `HEAD`. 137 138If you tried a merge which resulted in complex conflicts and 139want to start over, you can recover with `git reset --merge`. 140 141HOW CONFLICTS ARE PRESENTED 142--------------------------- 143 144During a merge, the working tree files are updated to reflect the result 145of the merge. Among the changes made to the common ancestor's version, 146non-overlapping ones (that is, you changed an area of the file while the 147other side left that area intact, or vice versa) are incorporated in the 148final result verbatim. When both sides made changes to the same area, 149however, git cannot randomly pick one side over the other, and asks you to 150resolve it by leaving what both sides did to that area. 151 152By default, git uses the same style as that is used by "merge" program 153from the RCS suite to present such a conflicted hunk, like this: 154 155------------ 156Here are lines that are either unchanged from the common 157ancestor, or cleanly resolved because only one side changed. 158<<<<<<< yours:sample.txt 159Conflict resolution is hard; 160let's go shopping. 161======= 162Git makes conflict resolution easy. 163>>>>>>> theirs:sample.txt 164And here is another line that is cleanly resolved or unmodified. 165------------ 166 167The area where a pair of conflicting changes happened is marked with markers 168`<<<<<<<`, `=======`, and `>>>>>>>`. The part before the `=======` 169is typically your side, and the part afterwards is typically their side. 170 171The default format does not show what the original said in the conflicting 172area. You cannot tell how many lines are deleted and replaced with 173Barbie's remark on your side. The only thing you can tell is that your 174side wants to say it is hard and you'd prefer to go shopping, while the 175other side wants to claim it is easy. 176 177An alternative style can be used by setting the "merge.conflictstyle" 178configuration variable to "diff3". In "diff3" style, the above conflict 179may look like this: 180 181------------ 182Here are lines that are either unchanged from the common 183ancestor, or cleanly resolved because only one side changed. 184<<<<<<< yours:sample.txt 185Conflict resolution is hard; 186let's go shopping. 187||||||| 188Conflict resolution is hard. 189======= 190Git makes conflict resolution easy. 191>>>>>>> theirs:sample.txt 192And here is another line that is cleanly resolved or unmodified. 193------------ 194 195In addition to the `<<<<<<<`, `=======`, and `>>>>>>>` markers, it uses 196another `|||||||` marker that is followed by the original text. You can 197tell that the original just stated a fact, and your side simply gave in to 198that statement and gave up, while the other side tried to have a more 199positive attitude. You can sometimes come up with a better resolution by 200viewing the original. 201 202 203HOW TO RESOLVE CONFLICTS 204------------------------ 205 206After seeing a conflict, you can do two things: 207 208 * Decide not to merge. The only clean-ups you need are to reset 209 the index file to the `HEAD` commit to reverse 2. and to clean 210 up working tree changes made by 2. and 3.; `git-reset --hard` can 211 be used for this. 212 213 * Resolve the conflicts. Git will mark the conflicts in 214 the working tree. Edit the files into shape and 215 'git add' them to the index. Use 'git commit' to seal the deal. 216 217You can work through the conflict with a number of tools: 218 219 * Use a mergetool. `git mergetool` to launch a graphical 220 mergetool which will work you through the merge. 221 222 * Look at the diffs. `git diff` will show a three-way diff, 223 highlighting changes from both the `HEAD` and `MERGE_HEAD` 224 versions. 225 226 * Look at the diffs from each branch. `git log --merge -p <path>` 227 will show diffs first for the `HEAD` version and then the 228 `MERGE_HEAD` version. 229 230 * Look at the originals. `git show :1:filename` shows the 231 common ancestor, `git show :2:filename` shows the `HEAD` 232 version, and `git show :3:filename` shows the `MERGE_HEAD` 233 version. 234 235 236EXAMPLES 237-------- 238 239* Merge branches `fixes` and `enhancements` on top of 240 the current branch, making an octopus merge: 241+ 242------------------------------------------------ 243$ git merge fixes enhancements 244------------------------------------------------ 245 246* Merge branch `obsolete` into the current branch, using `ours` 247 merge strategy: 248+ 249------------------------------------------------ 250$ git merge -s ours obsolete 251------------------------------------------------ 252 253* Merge branch `maint` into the current branch, but do not make 254 a new commit automatically: 255+ 256------------------------------------------------ 257$ git merge --no-commit maint 258------------------------------------------------ 259+ 260This can be used when you want to include further changes to the 261merge, or want to write your own merge commit message. 262+ 263You should refrain from abusing this option to sneak substantial 264changes into a merge commit. Small fixups like bumping 265release/version name would be acceptable. 266 267 268include::merge-strategies.txt[] 269 270CONFIGURATION 271------------- 272include::merge-config.txt[] 273 274branch.<name>.mergeoptions:: 275 Sets default options for merging into branch <name>. The syntax and 276 supported options are the same as those of 'git merge', but option 277 values containing whitespace characters are currently not supported. 278 279SEE ALSO 280-------- 281linkgit:git-fmt-merge-msg[1], linkgit:git-pull[1], 282linkgit:gitattributes[5], 283linkgit:git-reset[1], 284linkgit:git-diff[1], linkgit:git-ls-files[1], 285linkgit:git-add[1], linkgit:git-rm[1], 286linkgit:git-mergetool[1] 287 288Author 289------ 290Written by Junio C Hamano <gitster@pobox.com> 291 292 293Documentation 294-------------- 295Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>. 296 297GIT 298--- 299Part of the linkgit:git[1] suite