Resolving Merge Conflicts
Conflicts happen when Git can’t automatically combine changes from two branches. You must resolve them manually.
When Conflicts Occur
- Two branches modify the same line in a file
- One branch deletes a file the other modifies
- Both branches add a file with the same name
Conflict Markers
<<<<<<< HEAD
your changes on the current branch
=======
changes from the incoming branch
>>>>>>> feature
<<<<<<< HEAD— your current changes=======— divider>>>>>>> feature— incoming changes
How to Resolve
- Open the conflicted file
- Choose which changes to keep (or combine both)
- Delete the conflict markers
- Save the file
git add resolved-file.js
git commit -m "Resolve merge conflict in resolved-file.js"
Using a Merge Tool
git mergetool
Opens a visual diff tool to help resolve conflicts side by side.
Aborting a Merge
git merge --abort
Returns to the state before the merge started.
Best Practices
- Resolve conflicts soon after they appear
- Communicate with your team about conflicting changes
- Test your code after resolving conflicts
- Use
git diffto review changes before committing
Common Mistakes
- Accidentally deleting code from the other branch
- Forgetting to remove conflict markers
- Committing without
git addfirst - Not testing after resolution