Resolving a merge conflict

Start the Git merge tool (which is vimdiff, since we configured it earlier):

$ git mergetool

You will be treated to quite a light show, with four windows and a lot of colors thrown at you:

It's okay to be scared, but it's not as terrifying as it looks.

Local changes (master branch in this case) are in the upper-left window, followed by a closest common ancestor and the create-animal branch in the upper-right corner. The result of the merge is in the bottom window.

To get into more detail, from left to right, top to bottom:

  • LOCAL: This is file from the current branch (or whatever you're merging into)
  • BASE: The common ancestor—how the file looked before both changes took place
  • REMOTE: The file you are merging from another branch (no-dogs in this case)
  • MERGED: The merge result—this is what gets saved as output

In the MERGED window, you'll see conflict markers. You don't need to interact with them directly, but it's good to have a vague idea of what they mean. Conflict markers are identified by <<<<<<< and >>>>>>>:

<<<<<<< [LOCAL commit/branch]
[LOCAL change]
||||||| merged common ancestors
[BASE - closest common ancestor]
=======
[REMOTE change]
>>>>>>> [REMOTE commit/branch]

Since we have multiple files, simply running do (:diffget) or dp (:diffput) without arguments would not be enough.

Assuming you want to keep the REMOTE change (from the create-animal branch), move your cursor to the window with MERGED file (the one at the bottom). Now move the cursor to the next change (in addition to regular movement keys, you can use ]c and [c to move by changes). Now execute the following:

:diffget REMOTE

This will get the change from the REMOTE file and place it into the MERGED file. You can shorten these commands:

  • Get a REMOTE change using :diffg R
  • Get a BASE change using :diffg B
  • Get a LOCAL change using :diffg L

Repeat for every conflict. Once you're done addressing the conflicts, save the MERGED file and exit vimdiff (running :wqa would be the fastest way) to complete the merge (or move on to the next file if you have more conflicts).

Merge conflicts tend to leave .orig files in your working directory (for example, animal_farm.py.orig); feel free to discard those once you're done merging.

Don't forget to commit the merge results (git commit -m "Fixed a pesky merge conflict") once you're done.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.152.99