4.6.2 Dealing With Merge Conflicts

Sometimes, when merging from master into your branch, or from a branch into master, there will be merge conflicts. These are one or more areas within a file where there are conflicting sets of changes, and Git could not do the merge for you. In this case, the conflicted area will be delimited by the traditional conflict markers, ‘<<<’, ‘===’ and ‘>>>’.

Your mission then is to edit the file and resolve the conflict by fixing the order of additions (such as in a ChangeLog file), or fixing the code to take new changes into account.

Once you have done so, you tell Git that everything is OK using ‘git add’ and ‘git commit’:

$ git checkout feature/python        Move back to new, purely local branch
$ git rebase master                  ``Start over'' from current master
-| First, rewinding head to replay your work on top of it...
-| Applying: Demo change.
-| Using index info to reconstruct a base tree...
-| M	main.c
-| Falling back to patching base and 3-way merge...
-| Auto-merging main.c
-| CONFLICT (content): Merge conflict in main.c
-| error: Failed to merge in the changes.
-| Patch failed at 0001 Demo change.
-| Use 'git am --show-current-patch' to see the failed patch
-|
-| Resolve all conflicts manually, mark them as resolved with
-| "git add/rm <conflicted_files>", then run "git rebase --continue".
-| You can instead skip this commit: run "git rebase --skip".
-| To abort and get back to the state before "git rebase", run "git rebase --abort".
$ gvim main.c                        Edit the file and fix the problem
$ git add main.c                     Tell Git everything is OK now …
$ git commit                         … and it's settled
$ git rebase --continue              Continue the rebase

The git rebase --continue then continues the process of rebasing the current branch that we started in Rebasing A Local Branch. It’s not necessary if you are using ‘git merge’ (see Points to Remember).