What is a Merge Conflict?
A merge conflict happens when two branches change the same part of a file and Git can’t decide which version to keep.
You must resolve the conflict manually before completing the merge.
Why Merge Conflicts Happen
Conflicts usually occur when:
- Multiple branches modify the same lines in a file.
- Changes overlap in a way Git can’t automatically combine.
This often happens in collaborative projects or with long-lived feature branches.
How to Find and Resolve Merge Conflicts
Step 1: Merge the Branch
git merge feature-branch
If there are conflicts, Git will stop and list the affected files.
Step 2: Check Which Files Have Conflicts
git status
Step 3: See What Changed
git diff
This shows the conflicting parts so you can decide how to fix them.
Step 4: Edit the Files
Open each conflicted file. Git marks conflicts like this:
<<<<<<< HEAD
Your changes here
=======
Changes from feature-branch
>>>>>>> feature-branch
Edit to keep the content you want, and remove all conflict markers (<<<<<<<, =======, >>>>>>>).
Step 5: Mark the Conflict as Resolved
git add filename.txt
Step 6: Complete the Merge
If Git doesn’t auto-commit after resolving, finish manually:
git commit
Cancel the Merge
If you decide to stop and undo the merge:
git merge --abort
Use a Visual Merge Tool
You can launch a merge tool to help you choose between changes:
git mergetool
Keep Only One Side’s Changes
If you want to keep only your branch’s changes:
git checkout --ours filename.txt
Or keep only the incoming branch’s changes:
git checkout --theirs filename.txt
Troubleshooting & Best Practices
- Always remove conflict markers before marking a file as resolved.
- Use git merge --abort anytime you want to cancel and start over.
- Even after using git mergetool, you can still edit files manually if needed.
- Keep your branches up to date with git pull to reduce the chance of conflicts.