How to Resolve a Merge Conflict?
GitHub makes collaborating with many different people on a single piece of code or software much easier by managing the details involved in version control alongside your project's evolving history. This makes it especially useful for comparing code changes as they come in and sorting out differences between code submissions or "merges" before they affect the rest of your team's work. Unfortunately, GitHub's supreme powers of comparison are not completely infallible.
When someone attempts to merge code that’s been modified on the same line that a previous submission has changed, it can create a conundrum that even Git can’t figure out on its own. In these instances, you’ll encounter a merge conflict—a sort of clash between your code and someone else's—that you must manually resolve before you can move forward. There are a couple of ways to go about doing this, but they both apply to slightly different problems.
How you can resolve merge conflicts
Resolving whatever merge conflicts arise as quickly as possible comes down to first figuring out what files have been affected and then choosing an appropriate approach to take manual action. The two approaches at your disposal are resolving the conflict using GitHub or doing so via the command line.
Resolving a merge conflict on GitHub
GitHub provides a great tool for resolving simple merge conflicts, aptly named the "conflict editor." This editor simplifies the process of resolving basic merge conflicts by allowing you to choose what happens with proposed changes on the spot.
Making sense of conflict markers
The conflict editor presents conflicting changes using special markers that tell you where they originated and where they were supposed to be merged. These markers can be confusing at first, but they can be summed up succinctly:
- The
======
marker marks a division between the two versions of the same line in a file. - The top and bottom markers (
<<<<<<<
and>>>>>>>
) describe where updates came from and what updates they are conflicting with.
The top and bottom markers trade meanings, depending on the way your conflicting merge was attempted. In the case of a normal merge, the top shows your local changes, while the bottom shows changes that were made upstream in your project. When a conflict arises during an attempted rebase, the top will instead show your upstream changes, while the bottom shows topic branch changes.
Resolving the conflict
It’s entirely up to you how you go about resolving the conflict itself. You can choose to incorporate a new change altogether, remove one of the two that are presented, or pick pieces of each you would prefer to keep and get rid of the rest. Make the edits you think are best and mark the conflict as resolved to proceed with a final merge.
Resolving a merge conflict on the command line
Although GitHub's conflict editor can be very handy, it’s not always capable of helping you resolve complicated issues. In fact, anything more than a trivial issue must be handled via the command line or terminal on your local machine.
This means you’ll need to fire up a terminal window and navigate to your local Git repository. Once there, you can use git status
to find out what files have conflicts. When you manually review affected files in your code or text editor of choice, you should find the same types of conflict markers used for highlighting conflicts on GitHub. You can then modify the files in question yourself and add changes with git add
to resolve each issue.
The above process is largely the same when conflicts caused by deleted files arise. In those cases, you will need to choose whether a deleted file should ultimately be added back in or kept out. To add it to your commit, you can use the git add
command followed by the filename. To remove the file, you can use git rm
with the filename instead.
How to keep merge conflicts from happening
When it comes to mitigating merge conflicts and the productivity problems they cause, the best approach is prevention. There are plenty of things that you can do to keep merge conflicts from happening in your projects. Here are a few simple suggestions:
- Keep both your branches and your project’s modules as small as possible at all times.
- Avoid beautifying code beyond the scope of your intended changes.
- Communicate clearly and consistently with your team members.
Of course, one of the best ways that you can reduce the number of merge conflicts that come up in your code is to give Mergify a try. By streamlining the merging and reviewing process through thoughtful automation, Mergify can help prevent avoidable issues from wasting your team’s time. Request a demo today to get started.