Merge Conflict Resolution
Merge conflicts are an inevitable part of collaborative software development. They occur when Git is unable to automatically reconcile differences between two branches—usually because the same line in the same file was modified in different ways.
🔍 Understanding the Conflict
When a conflict occurs, Git stops the merge process and marks the problematic file. You can see which files are conflicted by running:
git statusFiles with conflicts will be listed under "Unmerged paths".
🧱 Anatomy of a Conflict Marker
Git inserts visual markers directly into your code to show you the competing changes:
<<<<<<< HEAD
This is the code as it exists in your current branch (e.g., main).
=======
This is the incoming code from the branch you are trying to merge (e.g., feature-branch).
>>>>>>> feature-branch<<<<<<< HEAD: Start of your local changes.=======: The separator between the two versions.>>>>>>> [branch_name]: End of the incoming changes.
🛠️ The Step-by-Step Resolution
- Open the File: Open the conflicted file in your code editor.
- Decide: Review both versions and choose which to keep, or manually combine them.
- Clean Up: Remove the conflict markers (
<<<<<<<,=======,>>>>>>>). - Stage the Fix:
git add <filename> - Finalize:
git commit -m "Resolve merge conflict in <filename>"
🛑 The "Eject" Button
If the conflict is too complex and you need to stop and regroup, you can completely reset the merge and return your repository to its pre-merge state:
git merge --abort[!TIP] Prevention is Better than Cure To minimize conflicts:
- Pull Frequently: Keep your local branches up-to-date with the team's work.
- Keep Commits Small: Atomic changes are much easier to merge automatically.
- Communicate: If you're refactoring a core file, let your team know!