Merge conflicts in GitHub as a new user can be tricky to resolve if you don’t know what you’re doing. We’ve made things easy for you by putting together how merge conflicts can happen, a couple of different ways you can solve them, and how you can reduce merge conflicts going forward.
What are Merge Conflicts?
When merging multiple branches, situations arise where you have conflicting file commits. In this scenario, Git asks you which changes you want to keep as part of the final merge.
Resolving Merge Conflicts in GitHub
There are two ways to resolve merge conflicts in GitHub, one is a simple competing line change, and the other requires solving the issue locally. Feel free to use the following jump links to navigate or just scroll.
- Resolving Through GitHub
- Resolving Locally
Resolving through GitHub
This is the simple way to resolve a merge conflict. An example of a competing line change would be if two developers updated one file in two separate branches. You can do this through GitHub’s conflict editor.
Step 1: Navigate to the Pull Request that needs to be resolved
Step 2: Click Resolve Conflict
This should open up GitHub’s web editor as shown below
Step 3: Fix the issue
Now it is up to you to resolve the merge conflict. GitHub uses ‘<<<<<<<’, ‘=======’, and ‘>>>>>>>’ as conflict markers, meaning that this denotes which part of the code is causing issues.
You can decide which part of the code you’d like to keep, if you’d like to keep both parts of the code, or if you don’t need any of the code at all. When you’re done editing the code, click ‘Mark as resolved’
Then click on the green ‘commit merge’ button, which should bring you to this screen.
Click ‘Merge pull request’ and ‘commit merge’ then you should be all set to delete the feature branch.
For more complex issues, you will need to resolve the merge conflicts locally through a tool like Visual Studio Code.
Step 1: Navigate to Terminal
Here’s what a merge conflict will look like in Visual Studio Code as an example.
Step 2: Resolve the conflict in the editor
Now is where you can fix the issue through a couple of options. You can click ‘Accept incoming’ to replace the current code with the incoming code. Click ‘Accept combination’ to combine the incoming and the current code. Click ‘Accept Current’ to ignore the new code and keep the code the same
Step 3: Commit Changes
Step 4: Publish Branch
Now that you’ve resolved the merge conflict in the code, click ‘Publish Branch’ on the left side to commit your changes to the main branch.
Here’s what the process will look like in terminal.
Step 5: Final Result
Now head back over to GitHub, where you’ll see that the changes have been made. It’s that simple.
Tips For Reducing Merge Conflicts
The best cure for anything is prevention. So, now that you know how to resolve merge conflicts, here are some tips to reduce the issue in the future.
Reduce the Number of Developers Working on the Same File
You may have heard the phrase “two heads are better than one,” but this isn’t always the case, especially not regarding coding. If you have multiple developers working on the same file through different branches, this will likely lead to merge conflicts. This is because two developers often have different approaches to solving the same issue.
Merge Branches More Frequently
When multiple developers are working on code, it’s inevitable that merge conflicts will happen. That’s why, it is best to merge branches as frequently as possible, that way the process should be relatively simple. If you wait until the last minute (which we’ve all been there) to merge all your branches, the issues become increasingly complex.