git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / Documentation / gitmergeconflicts.adoc
| 1 | gitmergeconflicts(7) |
| 2 | ==================== |
| 3 | |
| 4 | NAME |
| 5 | ---- |
| 6 | gitmergeconflicts - Guide to handling merge conflicts |
| 7 | |
| 8 | DESCRIPTION |
| 9 | ----------- |
| 10 | |
| 11 | Merge conflicts can happen during a `git merge`, `git rebase`, `git |
| 12 | cherry-pick`, `git pull`, `git revert`, or any other command that does |
| 13 | a merge. All of those commands use the same merge algorithm, and the |
| 14 | process for resolving a merge conflict is always very similar. |
| 15 | |
| 16 | There are 3 ways to handle a merge conflict: |
| 17 | |
| 18 | * Resolve the conflict. (see <<resolve,HOW TO RESOLVE A MERGE CONFLICT>> |
| 19 | below for details) |
| 20 | * Stop the operation and return your branch to its original state |
| 21 | with the appropriate `--abort` command, for example `git merge --abort` |
| 22 | or `git rebase --abort`. See <<git_status,EXAMPLE: GIT STATUS OUTPUT>> below |
| 23 | for how to find the command to run. |
| 24 | * Or (if you're in a multi-step operation like a rebase) skip the problematic |
| 25 | commit with the appropriate `--skip` command. |
| 26 | |
| 27 | WHAT IS A MERGE CONFLICT? |
| 28 | ------------------------- |
| 29 | |
| 30 | When Git merges two commits together, it looks at the changes that |
| 31 | each side has made and combines those changes. For example, if one side |
| 32 | edited lines 1-5 of `hello.py` and the other side edited lines 20-25 of |
| 33 | the same file, then it can easily combine them since there's no overlap. |
| 34 | |
| 35 | But if both sides edited overlapping lines of the same file (for example |
| 36 | one side edited lines 1-5 and the other edited lines 3-6), Git will |
| 37 | not try to guess how to combine those changes. This is called a "merge |
| 38 | conflict". |
| 39 | |
| 40 | When this happens, Git shows you both sides' edits and asks you to pick |
| 41 | how to resolve them. It: |
| 42 | |
| 43 | * Stages all of the files which were successfully merged |
| 44 | * For the files with conflicts, it marks them as conflicted, puts both |
| 45 | sides' edits in the file, and leaves <<markers, merge conflict markers>> |
| 46 | that you need to resolve. |
| 47 | |
| 48 | [NOTE] |
| 49 | Even if Git successfully merges both sides with no conflicts, it's |
| 50 | possible to end up with code that doesn't work. For example, this can |
| 51 | happen if one side renames a function, and the other side adds a call to |
| 52 | the old function. This is called a "semantic merge conflict". |
| 53 | |
| 54 | [[markers]] |
| 55 | MERGE CONFLICT MARKERS |
| 56 | ---------------------- |
| 57 | |
| 58 | When there's a merge conflict, Git will update the conflicted file |
| 59 | to include merge conflict markers `<<<<<<<`, `=======`, and `>>>>>>>`. |
| 60 | For example, here's a merge conflict where both sides edited a list of |
| 61 | fruits in different ways -- one side has "cherry" and the other side |
| 62 | has "banana": |
| 63 | |
| 64 | ---- |
| 65 | FRUITS = [ |
| 66 | "apple", |
| 67 | <<<<<<< HEAD |
| 68 | "cherry", |
| 69 | ======= |
| 70 | "banana", |
| 71 | >>>>>>> add-fruit |
| 72 | "mango", |
| 73 | "orange", |
| 74 | ] |
| 75 | ---- |
| 76 | |
| 77 | The lines from one side of the merge conflict is between `<<<<<<<` and |
| 78 | `=======`, and the lines from the other side is between `=======` and |
| 79 | `>>>>>>>`. See <<ours,"OURS" AND "THEIRS">> below for a full explanation |
| 80 | of which side is which. |
| 81 | |
| 82 | |
| 83 | [[resolve]] |
| 84 | HOW TO RESOLVE A MERGE CONFLICT |
| 85 | ------------------------------- |
| 86 | |
| 87 | The process for resolving a merge conflict is: |
| 88 | |
| 89 | 1. Run `git status` to get a list of files with merge conflicts. |
| 90 | 2. For each one, find the conflict markers |
| 91 | (the `<<<<<<<`, `=======`, `>>>>>>>`) and edit the file to |
| 92 | fix the conflict. |
| 93 | 3. Run `git add FILENAME` for each file to mark the conflict as resolved. |
| 94 | 4. Run the appropriate `--continue` command to continue the operation |
| 95 | that was interrupted by the conflict, for example `git merge --continue` |
| 96 | or `git rebase --continue`. See <<git_status,EXAMPLE: GIT STATUS OUTPUT>> |
| 97 | below for how to find the command to run. |
| 98 | + |
| 99 | Note: During a `git merge`, `git commit` and `git merge --continue` do |
| 100 | the same thing. |
| 101 | |
| 102 | |
| 103 | [[example]] |
| 104 | EXAMPLE OF RESOLVING A MERGE CONFLICT |
| 105 | ------------------------------------- |
| 106 | |
| 107 | If you see this in your code during a merge conflict: |
| 108 | |
| 109 | ---- |
| 110 | FRUITS = [ |
| 111 | "apple", |
| 112 | <<<<<<< HEAD |
| 113 | "cherry", |
| 114 | ======= |
| 115 | "banana", |
| 116 | >>>>>>> add-fruit |
| 117 | "mango", |
| 118 | "orange", |
| 119 | ] |
| 120 | ---- |
| 121 | |
| 122 | Then you might edit that part of the code like this, |
| 123 | which includes the fruits from both sides of the conflict: |
| 124 | |
| 125 | ---- |
| 126 | FRUITS = [ |
| 127 | "apple", |
| 128 | "banana", |
| 129 | "cherry", |
| 130 | "mango", |
| 131 | "orange", |
| 132 | ] |
| 133 | ---- |
| 134 | |
| 135 | |
| 136 | [[tools]] |
| 137 | TOOLS FOR HANDLING MERGE CONFLICTS |
| 138 | ---------------------------------- |
| 139 | |
| 140 | Here are some ways to get extra context while handling a merge conflict: |
| 141 | |
| 142 | * There are many graphical "merge tools" for Git, which will normally |
| 143 | show you the different versions of the code side by side. |
| 144 | If you have a mergetool configured, `git mergetool` will launch it. |
| 145 | See also `merge.tool` in linkgit:git-config[1] for a list of |
| 146 | the mergetools Git supports. |
| 147 | |
| 148 | * You can set the configuration variable `merge.conflictstyle=diff3`. |
| 149 | See <<diff3,DIFF3>> below for more. |
| 150 | |
| 151 | * `git log --merge --patch <filename>` will list all commits which |
| 152 | caused the merge conflict for `<filename>`, and the diff |
| 153 | of how they changed the file. |
| 154 | |
| 155 | * Look at the original files. `git show :1:filename` shows the |
| 156 | common ancestor, `git show :2:filename` shows the "ours" |
| 157 | version, and `git show :3:filename` shows the "theirs" |
| 158 | version. See <<ours,"OURS" AND "THEIRS">> below for more on |
| 159 | "ours" and "theirs". |
| 160 | |
| 161 | Here are some ways to track your progress while handling a conflict: |
| 162 | |
| 163 | * Use `git status` to get a list of files with conflicts |
| 164 | |
| 165 | * Use `git diff --check` to make sure you haven't left any merge |
| 166 | conflict markers in a file by accident. It will print "leftover |
| 167 | conflict marker" if it finds any. |
| 168 | |
| 169 | * Use `git diff AUTO_MERGE` to show what changes you've made so far to |
| 170 | resolve the conflicts. |
| 171 | See <<automerge,EXAMPLE OF USING `AUTO_MERGE`>> below for more. |
| 172 | |
| 173 | [[git_status]] |
| 174 | EXAMPLE: GIT STATUS OUTPUT |
| 175 | -------------------------- |
| 176 | |
| 177 | When you're in a merge conflict, you can find out what commands to run |
| 178 | to handle the conflict by running `git status`. |
| 179 | |
| 180 | For example, this `git status` output tells you that: |
| 181 | |
| 182 | * `git rebase --abort` will safely bring your branch back to its |
| 183 | original state |
| 184 | * you should run `git rebase --continue` when you're done resolving all |
| 185 | the conflicts |
| 186 | * the conflicts in `fruits.py` haven't been marked as resolved |
| 187 | |
| 188 | ---- |
| 189 | $ git status |
| 190 | You are currently rebasing branch 'main' on '58a9fcc'. |
| 191 | (fix conflicts and then run "git rebase --continue") |
| 192 | (use "git rebase --skip" to skip this patch) |
| 193 | (use "git rebase --abort" to check out the original branch) |
| 194 | |
| 195 | Unmerged paths: |
| 196 | (use "git restore --staged <file>..." to unstage) |
| 197 | (use "git add <file>..." to mark resolution) |
| 198 | both modified: fruits.py |
| 199 | ---- |
| 200 | |
| 201 | |
| 202 | [[diff3]] |
| 203 | DIFF3 |
| 204 | ----- |
| 205 | |
| 206 | By default, Git doesn't include the original code when formatting |
| 207 | a merge conflict. To include the original code, you can set the |
| 208 | configuration option `merge.conflictstyle` to `diff3`. |
| 209 | This extra context can make it much easier to understand what's |
| 210 | happening in a merge conflict. |
| 211 | |
| 212 | Here's an example of what a merge conflict would look like when using |
| 213 | `diff3`. There's an extra section (between `|||||||` and `=======`) |
| 214 | with the original code. |
| 215 | |
| 216 | ---- |
| 217 | FRUITS = [ |
| 218 | "apple", |
| 219 | <<<<<<< HEAD |
| 220 | "cherry", |
| 221 | "mango", |
| 222 | ||||||| 1c22e48 |
| 223 | "banana", |
| 224 | "cherry", |
| 225 | "mangoooo", |
| 226 | ======= |
| 227 | "banana", |
| 228 | "mango", |
| 229 | >>>>>>> add-fruit |
| 230 | "orange", |
| 231 | ] |
| 232 | ---- |
| 233 | |
| 234 | With this view, you can see that both sides fixed a spelling |
| 235 | mistake in "mango", and each removed one fruit from the list. |
| 236 | This makes it more clear how to resolve the merge conflict: since one |
| 237 | side removed `banana` and one side removed `cherry`, it would probably |
| 238 | make sense to remove both from the list, like this: |
| 239 | |
| 240 | ---- |
| 241 | FRUITS = [ |
| 242 | "apple", |
| 243 | "mango", |
| 244 | "orange", |
| 245 | ] |
| 246 | ---- |
| 247 | |
| 248 | [NOTE] |
| 249 | `zdiff3` is an alternative to `diff3` that makes the merge conflict |
| 250 | shorter by making it more ambiguous. See linkgit:git-config[1] for more. |
| 251 | |
| 252 | [[ours]] |
| 253 | "OURS" AND "THEIRS" |
| 254 | ------------------- |
| 255 | |
| 256 | Sometimes during a merge conflict, Git will use the terms "ours" and |
| 257 | "theirs" (or "us" and "them"). For example, `git status` might say that |
| 258 | a file was `deleted by us`. |
| 259 | |
| 260 | "Ours" and "theirs" are both commits: "ours" is the current |
| 261 | `HEAD` commit, and "theirs" is the other side being merged. |
| 262 | |
| 263 | The first part of a merge conflict (between `<<<<<<<` and `=======`) is |
| 264 | from the "ours" side, and the second part (between `=======` and |
| 265 | `>>>>>>>`) is from the "theirs" side. |
| 266 | |
| 267 | ---- |
| 268 | FRUITS = [ |
| 269 | "apple", |
| 270 | <<<<<<< HEAD |
| 271 | "cherry", <- ours |
| 272 | ======= |
| 273 | "banana", <- theirs |
| 274 | >>>>>>> add-fruit |
| 275 | "mango", |
| 276 | "orange", |
| 277 | ] |
| 278 | ---- |
| 279 | |
| 280 | During a rebase, it can seem "upside down" because the "ours" commit is |
| 281 | from the branch you're rebasing on (for instance `main` in `git rebase |
| 282 | main`). |
| 283 | |
| 284 | These terms in Git all mean the same thing when dealing with a merge |
| 285 | conflict: |
| 286 | |
| 287 | * "common ancestor" and "base". The files from this commit are "in stage 1". |
| 288 | * "ours", "us", and `HEAD`. The files from this commit are "in stage 2". |
| 289 | * "theirs", "them". The files from this commit are "in stage 3". |
| 290 | |
| 291 | If you're confused about what something like "deleted by us" means, it's |
| 292 | often easiest to use some of the tools from |
| 293 | <<tools,TOOLS FOR HANDLING MERGE CONFLICTS>> above to get more context. |
| 294 | Finding the commit that deleted the file and seeing why is usually more |
| 295 | helpful than trying to abstractly reason through what "us" means. |
| 296 | |
| 297 | [[automerge]] |
| 298 | EXAMPLE OF USING `AUTO_MERGE` |
| 299 | ----------------------------- |
| 300 | |
| 301 | `git diff AUTO_MERGE` will show what changes you've made so far to |
| 302 | resolve conflicts. `AUTO_MERGE` is a reference that Git creates during a |
| 303 | merge. It contains the result of running the merge algorithm. |
| 304 | |
| 305 | For example, if we resolved the conflict by adding both "banana" and |
| 306 | "cherry" in order, the diff would look like this: |
| 307 | |
| 308 | ---- |
| 309 | FRUITS = [ |
| 310 | "apple", |
| 311 | -<<<<<<< HEAD |
| 312 | - "cherry", |
| 313 | -======= |
| 314 | "banana", |
| 315 | ->>>>>>> add-fruit |
| 316 | + "cherry", |
| 317 | "mango", |
| 318 | "orange", |
| 319 | ] |
| 320 | ---- |
| 321 | |
| 322 | [NOTE] |
| 323 | `AUTO_MERGE` is only set if you're using the default Git merge algorithm. |
| 324 | |
| 325 | |
| 326 | SEE ALSO |
| 327 | -------- |
| 328 | |
| 329 | linkgit:git-revert[1] |
| 330 | linkgit:git-merge[1] |
| 331 | linkgit:git-rebase[1] |
| 332 | linkgit:git-cherry-pick[1] |
| 333 | linkgit:git-pull[1] |
| 334 | linkgit:git-diff[1] |
| 335 | |
| 336 | GIT |
| 337 | --- |
| 338 | |
| 339 | Part of the linkgit:git[1] suite |