git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / Documentation / git-merge.adoc
| 1 | git-merge(1) |
| 2 | ============ |
| 3 | |
| 4 | NAME |
| 5 | ---- |
| 6 | git-merge - Join two or more development histories together |
| 7 | |
| 8 | |
| 9 | SYNOPSIS |
| 10 | -------- |
| 11 | [synopsis] |
| 12 | git merge [-n] [--stat] [--compact-summary] [--no-commit] [--squash] [--[no-]edit] |
| 13 | [--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]] |
| 14 | [--[no-]allow-unrelated-histories] |
| 15 | [--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] |
| 16 | [--into-name <branch>] [<commit>...] |
| 17 | git merge (--continue | --abort | --quit) |
| 18 | |
| 19 | DESCRIPTION |
| 20 | ----------- |
| 21 | Incorporates changes from the named commits (since the time their |
| 22 | histories diverged from the current branch) into the current |
| 23 | branch. This command is used by `git pull` to incorporate changes |
| 24 | from another repository and can be used by hand to merge changes |
| 25 | from one branch into another. |
| 26 | |
| 27 | Assume the following history exists and the current branch is |
| 28 | `master`: |
| 29 | |
| 30 | ------------ |
| 31 | A---B---C topic |
| 32 | / |
| 33 | D---E---F---G master |
| 34 | ------------ |
| 35 | |
| 36 | Then `git merge topic` will replay the changes made on the |
| 37 | `topic` branch since it diverged from `master` (i.e., `E`) until |
| 38 | its current commit (`C`) on top of `master`, and record the result |
| 39 | in a new commit along with the names of the two parent commits and |
| 40 | a log message from the user describing the changes. Before the operation, |
| 41 | `ORIG_HEAD` is set to the tip of the current branch (`G`). |
| 42 | |
| 43 | ------------ |
| 44 | A---B---C topic |
| 45 | / \ |
| 46 | D---E---F---G---H master |
| 47 | ------------ |
| 48 | |
| 49 | A merge stops if there's a conflict that cannot be resolved |
| 50 | automatically or if `--no-commit` was provided when initiating the |
| 51 | merge. At that point you can run `git merge --abort` or `git merge |
| 52 | --continue`. See linkgit:gitmergeconflicts[7] |
| 53 | (or `git help mergeconflicts`) for a guide to handling merge conflicts. |
| 54 | |
| 55 | `git merge --abort` will abort the merge process and try to reconstruct |
| 56 | the pre-merge state. However, if there were uncommitted changes when the |
| 57 | merge started (and especially if those changes were further modified |
| 58 | after the merge was started), `git merge --abort` will in some cases be |
| 59 | unable to reconstruct the original (pre-merge) changes. Therefore: |
| 60 | |
| 61 | WARNING: Running `git merge` with non-trivial uncommitted changes is |
| 62 | discouraged: while possible, it may leave you in a state that is hard to |
| 63 | back out of in the case of a conflict. |
| 64 | |
| 65 | OPTIONS |
| 66 | ------- |
| 67 | :git-merge: 1 |
| 68 | |
| 69 | include::merge-options.adoc[] |
| 70 | |
| 71 | `-m <msg>`:: |
| 72 | Set the commit message to be used for the merge commit (in |
| 73 | case one is created). |
| 74 | + |
| 75 | If `--log` is specified, a shortlog of the commits being merged |
| 76 | will be appended to the specified message. |
| 77 | + |
| 78 | The `git fmt-merge-msg` command can be |
| 79 | used to give a good default for automated `git merge` |
| 80 | invocations. The automated message can include the branch description. |
| 81 | |
| 82 | `--into-name <branch>`:: |
| 83 | Prepare the default merge message as if merging to the branch |
| 84 | _<branch>_, instead of the name of the real branch to which |
| 85 | the merge is made. |
| 86 | |
| 87 | `-F <file>`:: |
| 88 | `--file=<file>`:: |
| 89 | Read the commit message to be used for the merge commit (in |
| 90 | case one is created). |
| 91 | + |
| 92 | If `--log` is specified, a shortlog of the commits being merged |
| 93 | will be appended to the specified message. |
| 94 | |
| 95 | include::rerere-options.adoc[] |
| 96 | |
| 97 | `--overwrite-ignore`:: |
| 98 | `--no-overwrite-ignore`:: |
| 99 | Silently overwrite ignored files from the merge result. This |
| 100 | is the default behavior. Use `--no-overwrite-ignore` to abort. |
| 101 | |
| 102 | `--abort`:: |
| 103 | Abort the current conflict resolution process, and |
| 104 | try to reconstruct the pre-merge state. If an autostash entry is |
| 105 | present, apply it to the worktree. |
| 106 | + |
| 107 | If there were uncommitted worktree changes present when the merge |
| 108 | started, `git merge --abort` will in some cases be unable to |
| 109 | reconstruct these changes. It is therefore recommended to always |
| 110 | commit or stash your changes before running `git merge`. |
| 111 | + |
| 112 | `git merge --abort` is equivalent to `git reset --merge` when |
| 113 | `MERGE_HEAD` is present unless `MERGE_AUTOSTASH` is also present in |
| 114 | which case `git merge --abort` applies the stash entry to the worktree |
| 115 | whereas `git reset --merge` will save the stashed changes in the stash |
| 116 | list. |
| 117 | |
| 118 | `--quit`:: |
| 119 | Forget about the current merge in progress. Leave the index |
| 120 | and the working tree as-is. If `MERGE_AUTOSTASH` is present, the |
| 121 | stash entry will be saved to the stash list. |
| 122 | |
| 123 | `--continue`:: |
| 124 | After a `git merge` stops due to conflicts you can conclude the |
| 125 | merge by running `git merge --continue` (see "HOW TO RESOLVE |
| 126 | CONFLICTS" section below). |
| 127 | |
| 128 | `<commit>...`:: |
| 129 | Commits, usually other branch heads, to merge into our branch. |
| 130 | Specifying more than one commit will create a merge with |
| 131 | more than two parents (affectionately called an Octopus merge). |
| 132 | + |
| 133 | If no commit is given from the command line, merge the remote-tracking |
| 134 | branches that the current branch is configured to use as its upstream. |
| 135 | See also the configuration section of this manual page. |
| 136 | + |
| 137 | When `FETCH_HEAD` (and no other commit) is specified, the branches |
| 138 | recorded in the `.git/FETCH_HEAD` file by the previous invocation |
| 139 | of `git fetch` for merging are merged to the current branch. |
| 140 | |
| 141 | |
| 142 | PRE-MERGE CHECKS |
| 143 | ---------------- |
| 144 | |
| 145 | Before applying outside changes, you should get your own work in |
| 146 | good shape and committed locally, so it will not be clobbered if |
| 147 | there are conflicts. See also linkgit:git-stash[1]. |
| 148 | `git pull` and `git merge` will stop without doing anything when |
| 149 | local uncommitted changes overlap with files that `git pull`/`git |
| 150 | merge` may need to update. |
| 151 | |
| 152 | To avoid recording unrelated changes in the merge commit, |
| 153 | `git pull` and `git merge` will also abort if there are any changes |
| 154 | registered in the index relative to the `HEAD` commit. (Special |
| 155 | narrow exceptions to this rule may exist depending on which merge |
| 156 | strategy is in use, but generally, the index must match `HEAD`.) |
| 157 | |
| 158 | If all named commits are already ancestors of `HEAD`, `git merge` |
| 159 | will exit early with the message "Already up to date." |
| 160 | |
| 161 | FAST-FORWARD MERGE |
| 162 | ------------------ |
| 163 | |
| 164 | Often the current branch head is an ancestor of the named commit. |
| 165 | This is the most common case especially when invoked from `git |
| 166 | pull`: you are tracking an upstream repository, you have committed |
| 167 | no local changes, and now you want to update to a newer upstream |
| 168 | revision. In this case, a new commit is not needed to store the |
| 169 | combined history; instead, the `HEAD` (along with the index) is |
| 170 | updated to point at the named commit, without creating an extra |
| 171 | merge commit. |
| 172 | |
| 173 | This behavior can be suppressed with the `--no-ff` option. |
| 174 | |
| 175 | TRUE MERGE |
| 176 | ---------- |
| 177 | |
| 178 | Except in a fast-forward merge (see above), the branches to be |
| 179 | merged must be tied together by a merge commit that has both of them |
| 180 | as its parents. |
| 181 | |
| 182 | A merged version reconciling the changes from all branches to be |
| 183 | merged is committed, and your `HEAD`, index, and working tree are |
| 184 | updated to it. It is possible to have modifications in the working |
| 185 | tree as long as they do not overlap; the update will preserve them. |
| 186 | |
| 187 | When it is not obvious how to reconcile the changes, the following |
| 188 | happens: |
| 189 | |
| 190 | 1. The `HEAD` pointer stays the same. |
| 191 | 2. The `MERGE_HEAD` ref is set to point to the other branch head. |
| 192 | 3. Paths that merged cleanly are updated both in the index file and |
| 193 | in your working tree. |
| 194 | 4. For conflicting paths, the index file records up to three |
| 195 | versions: stage 1 stores the version from the common ancestor, |
| 196 | stage 2 from `HEAD`, and stage 3 from `MERGE_HEAD` (you |
| 197 | can inspect the stages with `git ls-files -u`). The working |
| 198 | tree files contain the result of the merge operation; i.e. 3-way |
| 199 | merge results with familiar conflict markers +<<<+ `===` +>>>+. |
| 200 | 5. A ref named `AUTO_MERGE` is written, pointing to a tree |
| 201 | corresponding to the current content of the working tree (including |
| 202 | conflict markers for textual conflicts). Note that this ref is only |
| 203 | written when the `ort` merge strategy is used (the default). |
| 204 | 6. No other changes are made. In particular, the local |
| 205 | modifications you had before you started merge will stay the |
| 206 | same and the index entries for them stay as they were, |
| 207 | i.e. matching `HEAD`. |
| 208 | |
| 209 | If you tried a merge which resulted in complex conflicts and |
| 210 | want to start over, you can recover with `git merge --abort`. |
| 211 | |
| 212 | MERGING TAG |
| 213 | ----------- |
| 214 | |
| 215 | When merging an annotated (and possibly signed) tag, Git always |
| 216 | creates a merge commit even if a fast-forward merge is possible, and |
| 217 | the commit message template is prepared with the tag message. |
| 218 | Additionally, if the tag is signed, the signature check is reported |
| 219 | as a comment in the message template. See also linkgit:git-tag[1]. |
| 220 | |
| 221 | When you want to just integrate with the work leading to the commit |
| 222 | that happens to be tagged, e.g. synchronizing with an upstream |
| 223 | release point, you may not want to make an unnecessary merge commit. |
| 224 | |
| 225 | In such a case, you can "unwrap" the tag yourself before feeding it |
| 226 | to `git merge`, or pass `--ff-only` when you do not have any work on |
| 227 | your own. e.g. |
| 228 | |
| 229 | ---- |
| 230 | git fetch origin |
| 231 | git merge v1.2.3^0 |
| 232 | git merge --ff-only v1.2.3 |
| 233 | ---- |
| 234 | |
| 235 | EXAMPLES |
| 236 | -------- |
| 237 | |
| 238 | * Merge branches `fixes` and `enhancements` on top of |
| 239 | the current branch, making an octopus merge: |
| 240 | + |
| 241 | ------------------------------------------------ |
| 242 | $ git merge fixes enhancements |
| 243 | ------------------------------------------------ |
| 244 | |
| 245 | * Merge branch `obsolete` into the current branch, using `ours` |
| 246 | merge strategy: |
| 247 | + |
| 248 | ------------------------------------------------ |
| 249 | $ git merge -s ours obsolete |
| 250 | ------------------------------------------------ |
| 251 | |
| 252 | * Merge branch `maint` into the current branch, but do not make |
| 253 | a new commit automatically: |
| 254 | + |
| 255 | ------------------------------------------------ |
| 256 | $ git merge --no-commit maint |
| 257 | ------------------------------------------------ |
| 258 | + |
| 259 | This can be used when you want to include further changes to the |
| 260 | merge, or want to write your own merge commit message. |
| 261 | + |
| 262 | You should refrain from abusing this option to sneak substantial |
| 263 | changes into a merge commit. Small fixups like bumping |
| 264 | release/version name would be acceptable. |
| 265 | |
| 266 | |
| 267 | include::merge-strategies.adoc[] |
| 268 | |
| 269 | CONFIGURATION |
| 270 | ------------- |
| 271 | |
| 272 | `branch.<name>.mergeOptions`:: |
| 273 | Sets default options for merging into branch _<name>_. The syntax and |
| 274 | supported options are the same as those of `git merge`, but option |
| 275 | values containing whitespace characters are currently not supported. |
| 276 | |
| 277 | include::includes/cmd-config-section-rest.adoc[] |
| 278 | |
| 279 | include::config/merge.adoc[] |
| 280 | |
| 281 | SEE ALSO |
| 282 | -------- |
| 283 | linkgit:git-fmt-merge-msg[1], linkgit:git-pull[1], |
| 284 | linkgit:gitattributes[5], |
| 285 | linkgit:git-reset[1], |
| 286 | linkgit:git-diff[1], linkgit:git-ls-files[1], |
| 287 | linkgit:git-add[1], linkgit:git-rm[1], |
| 288 | linkgit:git-mergetool[1] |
| 289 | linkgit:gitmergeconflicts[7] |
| 290 | |
| 291 | GIT |
| 292 | --- |
| 293 | Part of the linkgit:git[1] suite |