How Do I Resolve Merge Conflicts in git/GitHub/GitLab?

What’s a merge conflict?

One of the big things that trips up a lot of new users of git or other version control systems is conflicts. Usually, when two parallel versions of the code you (and/or your collaborators) are working on have different changes, git can “merge” them seamlessly. This is one of, if not the, best and most useful features of git. For example, if you delete line 25 of a script, and your collaborator edits line 99 and adds a few lines after it, git can merge those two sets of changes into a single script. However, sometimes there are merge conflicts: let’s say you both edited line 25. Git has no way to know which version it should pick when merging — this is a merge conflict.

merge conflict

Attempting to merge two conflicted versions of a file

The most important thing to remember is that no matter how nasty the conflicts look on the surface, you can always undo and start fresh. That’s the beauty of version control systems.

There are already tons of good resources out there about how to resolve conflicts in version control. This is a semi-curated list a few of the best resources that you can refer to if you see the dreaded merge conflict message.

A few words to the wise

Word to the wise #1: Usually it’s a good idea to try stackoverflow first. Unfortunately in this case, the top Google hits for basic git questions often lead to old questions with many competing, confusing, and outdated answers. Be wary of that! You can still find good answers on SO, but the “official” documentation linked below is often more reliable.

Word to the wise #2: A very common source of merge conflicts comes from the way text files are treated in different operating systems. In particular, Windows codes the end of each line of a text file with the CRLF character, while Linux-based systems including Mac OS code them with a LF character. (In case you are curious: CRLF stands for “Carriage Return Line Feed,” which will only make sense for those of you who’ve used a typewriter.) So you might get a merge conflict without even editing a file, if you are merging from repositories on computers with different operating systems. That’s also something to be on the lookout for. If you often run into this problem, here is a way to configure git to avoid it.


The ultimate source of line-ending merge conflicts?!?


Some resources for learning how to fix merge conflicts

These links should be a good start for advice on resolving merge conflicts. If you run into a truly thorny conflict, the SESYNC cyberhelp team will help you resolve it!