Skip to Content
A stick figure smiling

Here's a preview from my zine, How Git Works! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

Image of a comic. To read the full HTML alt text, click "read the transcript".
read the transcript!

merge conflicts happen because both branches edited the same lines of code

An illustration showing a merge algorithm, represented by a box with a mischievous expression. It has a thought bubble with three sequences of symbols. One reads dot, triangle, circle with top half filled in. This one has arrows coming out from it pointing to the two other sequences of symbols: one is plus sign, triangle, circle with top half filled in, the second is dot, triangle, circle with right half filled in. The merge algorithm is generating a sequence of: plus sign AND dot, with question marks around it, triangle, circle with right half filled in.

some ways to resolve merge conflicts

  • edit the weird text file by hand often the easiest way!

  • use a dedicated merge conflict tool you can configure git so that git mergetool opens conflicts in your favourite tool. I like meld on Linux.

  • abort the merge and rewrite the code you were merging from scratch
    might be easier if there was a big refactor! You can do this with git merge --abort

  • if the conflict is in an autogenerated file, delete and regenerate it
    great for package-lock.json in node!

  • go have a conversation with the other person about what to do

the weird text file

Git merge conflicts are confusing because they’re not displayed in a consistent way:

(sad face) the code from the branch you started on is:

  • at the top if you merged
  • at the bottom if you rebased

(sad face) git often won’t give you the branch name that the code comes from

Tiny illustration of a sad stick figure saying “why?”

<<<<<<< HEAD
def parse(input):   
return input.split("\n")   
||||||| b9447fc   
def parse(input):   
return input.split("\n\n")   
=======   
def parse(text):   
return text.split("\n\n")   
>>>>>>> a29b3cf   

the first three lines are the top

the part after the “=======” is the bottom

the rest is the original (configure merge.conflictstyle diff3 to get this)

finishing up

To finish, you need to run one of:

  • git commit
    (for git merge)
  • git rebase --continue
    (for git rebase)
  • git cherry-pick --continue
    (git cherry-pick)
  • git revert --continue
    (for git revert)

Before that, I might:

  • look at my changes with git diff main
  • check for unresolved conflicts with git diff --check