24 Handling Conflicts

You can’t develop a project with other developers without generating conflicting versions of code from time to time. Git goes a long way toward helping you resolve those automatically, but sometimes it can’t.

For example, the code shown on the opposite page shows two different solutions to the same problem. You must tell Git how to fix the conflict because it doesn’t know which version is correct. They both make changes to the exact same lines of code. You can find out which file or files have conflicts in two ways: the output from the failed git merge, or git status.

You can find conflicts within a file by looking for <<<<<<<—seven lesser than signs—with a commit such as HEAD. The original code—the code inside the branch you’re merging into—is located between the 7 < and ========. The new code is between that marker and >>>>>>>, followed by the name of branch (or commit ID, tag, and so on) you were merging in.

How you deal with the conflict and determine which version to use is up to you. You can choose one version over the other, use one and then the other, combine the two changes together, or completely remove them both.

Once you’ve resolved the conflict, call git add to stage the new changes and then git commit to commit it. You might not have anything to commit, however. For example, if you use all the changes from your local branch, Git doesn’t need to create a new commit. Nothing has changed.

You can also abort a conflict at any time before you commit the changes by resetting your working tree to ORIG_HEAD. See Task 37, Resetting Staged Changes and Commits for more about git reset.

Here’s an example of how a conflict can happen between two branches:

images/conflict.png
What To Do...
  • See an example merge conflict.

    The basics of the error message are the same, but the files are always different. For example, here’s a conflict in a repository for one of my projects:

     
    prompt>​ git merge added_raw
     
    Auto-merging dolt.py
     
    CONFLICT (content): Merge conflict in dolt.py
     
    Automatic merge failed; fix conflicts and then commit the result.

    You can find the conflict inside the file by looking for the special markup that Git adds, <<<<<<< followed by a commit (most of the time HEAD. Here’s the conflict in the previous file:

     
    <<<<<<< HEAD
     
    def _handle_response(self, response, data=[], raw=False):
     
    return data if raw else simplejson.loads(data)
     
    =======
     
    def _handle_response(self, response, raw=False, data=[]):
     
    if raw:
     
    return data
     
    return simplejson.loads(data)
     
    >>>>>>> added_raw
  • Commit a fixed conflict.

    You can use git status to see whether the conflict still exists:

     
    # On branch master
     
    # Unmerged paths:
     
    # (use "git add/rm <file>..." as appropriate to mark resolution)
     
    #
     
    # both modified: dolt.py
     
    #

    Once the conflict is resolved, call git add and git commit like you normally would.

Related Tasks

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

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