Last Updated: January 3, 2026
Resolving conflicts in Git can feel daunting, but with the right understanding and techniques, you can navigate them with ease. When two branches have changes that overlap, Git is unable to automatically merge them, leading to conflicts that require your attention.
This chapter will demystify the process of resolving these conflicts, providing you with practical strategies and insights to handle them effectively.
When you encounter a merge conflict, Git marks the conflicting areas in your files using conflict markers. These markers help you identify which sections of code are in dispute.
The conflict markers look like this:
<<<<<<< HEAD: This marks the beginning of your changes.=======: This separates your changes from the incoming changes.>>>>>>> branch-name: This indicates the end of the incoming changes from the branch you're trying to merge.When resolving conflicts, your first step is to carefully review these markers. You need to decide which changes to keep, whether to combine them, or to create a new solution entirely.
Always make a backup of your changes before resolving conflicts. Use git stash to save your current work if needed.
Conflicts can be resolved in several ways, depending on the nature of the changes and the desired outcome. Here are three common strategies:
This is the most straightforward approach. You manually edit the file, choosing which changes to keep.
After saving, you need to stage the resolved file:
Finally, commit the changes:
Many developers prefer using a merge tool for visual assistance in resolving conflicts.
Set up your preferred merge tool. For example, you can configure kdiff3:
When a conflict arises, invoke the merge tool:
The merge tool will show you the conflicting changes side by side, allowing for an easier visual resolution.
Once you finish resolving the conflicts in the tool, remember to stage and commit your changes.
In some cases, you may decide that the incoming changes are the most appropriate. You can choose to accept them outright using:
After this command, stage and commit the file as described earlier. This approach can be useful if the incoming changes are deemed more relevant or correct.
Be careful when using --theirs or --ours options. They apply to the entire file, so ensure that you're certain about discarding your changes.
Understanding the scenarios where conflicts arise can help you prepare better. Here are some frequent situations:
When two developers modify the same line in a file, a conflict occurs. For example, if Alice and Bob both change a line in app.js, their changes will conflict during a merge.
During the merge, Git will indicate a conflict, requiring resolution.
If one branch deletes a file while another modifies it, a conflict will arise. This can happen when a developer decides a feature is unnecessary and removes the file, but another developer has already made changes to it.
When this happens, you’ll need to determine if the file should be restored, kept, or modified based on the context provided by both branches.
Sometimes, a merge conflict may involve several files. Git will mark each conflicted file, allowing you to resolve them individually. Use the git status command to see all files in conflict:
This command will list files that require your attention, making it easier to tackle them systematically.
If you frequently find yourself dealing with merge conflicts, you can automate some common resolutions.
git rerereGit has a feature called rerere (reuse recorded resolution), which can help automate conflict resolution. To enable it, run:
With rerere enabled, Git remembers how you resolved conflicts and applies the same resolution when similar conflicts occur in the future. This can save you time and reduce frustration.
While rerere is powerful, it is essential to verify that the automatic resolutions are still appropriate for the context.
Another preventive measure against conflicts is regularly pulling changes from the remote repository. This practice keeps your local branch up to date and minimizes the chances of conflicts when you eventually merge.
Regular communication with your team about ongoing changes also helps align efforts and reduces overlap.
Sometimes, things don’t go as planned during conflict resolution. Understanding recovery options can save you headaches.
If you realize that the merge is too complicated or the conflicts are overwhelming, you can abort the merge process:
This command stops the merge and returns your branch to its previous state, allowing you to reconsider your approach.
To reduce conflicts, encourage a practice of keeping commits small and focused. This means making changes in discrete, logical chunks and merging them often. Smaller changes are less likely to overlap, making conflicts less frequent.
When working in a team, communication is key. Regularly discuss changes and coordinate efforts, especially when working on shared files. This can significantly reduce the likelihood of conflicts.
If you know a teammate is working on a critical feature, it might be worth checking in before making substantial changes to related files.
Now that you understand how to resolve merge conflicts effectively, you are ready to explore the various merge strategies Git offers.
In the next chapter, we will look at how different strategies can affect your workflow and the outcomes of your merges. You’ll gain insights into choosing the right strategy for your project's needs, ensuring smooth collaboration among team members.