Fixing Git Merge Conflicts

Git merge conflicts can be an absolute pain. Sometimes despite your best efforts you end up with conflicts when you create a Pull Request (PR) with your new feature. Just when you think you have finished with the current Product Backlog Item you have to go back and fix conflicts. It is a common issue especially with larger development teams where there can be several PRs a day. In this article we will have a look at how to best avoid getting conflicts in the first place. How to resolve them once they do appear and hopefully save you some time in the future!

How To Avoid Getting Merge Conflicts

The best way to avoid getting merge conflicts is to always stay up to date with the branch that you are planning on merging into. For example before you start working on your new feature for the Development branch, you should pull the latest from Development. Then branch once you are happy you have the latest code and begin working.

This will ensure that you are working with the most up to date code and reduces the chances of having any conflicts. If there are conflicts it should also drastically reduce the amount of time you may need to spend resolving them.

Ensure Work Is Abstracted

When working with bigger development teams ensure that the work you are all doing is properly abstracted. If it is then there should’t be a time where two or more people are making changes to the same file. Chances are that if two or more people are working on the same file, the application has a bad design and you should look at re-factoring the problem class or component.

This way even if there are multiple people creating PRs every day it should reduce the likeliness of merge conflicts. When there conflicts they will most likely be contained to project solution files which are very easy to resolve if required.

Setting Up For Conflict Resolution Success

It is probably unavoidable and you will eventually get a merge conflict that will be big and difficult to resolve. I will provide you with some tips here to hopefully make the process as painless as possible. All steps should be done on your local machine repository:

  1. Set current branch to the branch you want to merge into
  2. Pull latest from that branch
  3. Switch current branch back to your feature branch
  4. Create a local merge from the parent branch into your feature branch
  5. Depending on the tool you are using this may look very different. I personally tend to use Visual Studio Code. In there it flags the conflicted files and allows you to select which bits from which branch you want to keep or override. There is no best way to do this or no best tool. They are all fiddly in areas. Make sure you are comfortable with the one you use.
  6. It is important to check the files you have resolved to make sure you are happy with the conflict resolution before committing anything.
  7. Run any unit tests before commit

The above steps will hopefully allow you to have a process in mind when resolving merge conflicts. It should also give you a piece of mind that both your work and the conflicted work are both in the solution after resolving.

The Coding Hub was created in 2016 to provide customisable and professionally built websites to customers. In addition to this we provide training material.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store