Merge Conflicts
Although small batch work with short-lived branches and frequent merges improve flow, inevitable teams will encounter a merge conflict. When a conflict arises, more care, communication, and collaboration are needed to merge - this is especially true with large teams.
Merge conflicts happen¶
Merge conflicts occur when developers work in parallel and change the same lines of code, resulting in changes on both the source and target branches. Git is really good at tracking and integrating changes automatically without any merge conflict. However, there is no way to automatically resolves changes when two people want to make separate changes to the same code lines.
When these conditions are met in parallel development, a merge conflict will surface: 1. Similar starting point in target branch 2. Changes same code file 3. Changes to same lines of code file 4. Code is merged into same target branch
In this situataion, a race condition will occur where the first one to merge will merge successfully, and the second to merge will encounter a merge conflict.
Resolving a conflict¶
The reality is that merge conflicts will happen and the team should be prepared and know how to embrace them. To resolve them, teams should be careful to logically choose the correct outcome and not blindly accept one change over another. Use local Git tools to continue development and determine the best solution. When deciding which changes to take, consider that you can select the first change, the second change, or a combination of both changes. It will require pairing to determine the best choice.
- Communicate ahead of time to understand what files are needed for which features
- Fix locally, not on the Git server
- Pair with other developers to ensure correct changes are made
- Pull target branch from remote into your branch to reduce conflicts
- Learn how to utilize your Git tools
Can I avoid merge conflicts?¶
Frequently merging changes with short-lived branches and close collaboration will mitigate the frequency of merge conflicts. However, teams will invariably encounter them and should be comfortable resolving them.