Merge Conflicts — Resolving Differences
A merge conflict happens when two branches modify the same part of the same file. Git can't automatically decide which change to keep, so it asks you to resolve it manually.
When Do Conflicts Happen?
main: echo "hello" > greet.txt
feature: echo "hi" > greet.txt
Both branches changed greet.txt differently. When you merge, Git doesn't know which version you want.
What a Conflict Looks Like
Git marks the conflicting sections in the file:
<<<<<<< HEAD
hello
=======
hi
>>>>>>> feature
- Above
=======: Your current branch's version (HEAD/main) - Below
=======: The incoming branch's version (feature)
How to Resolve
- Edit the file to choose what you want (remove the markers)
- Stage the resolved file:
git add greet.txt - Complete the merge:
git commit -m "Resolve conflict"
Try it!
Create a conflict and resolve it:
- Create a file and commit:
echo "hello" > greet.txtthengit add greet.txtthengit commit -m "Add greeting" - Create a branch:
git checkout -b feature - Change the file:
echo "hi there" > greet.txtthengit add greet.txtthengit commit -m "Change greeting on feature" - Go back to main:
git checkout main - Make a different change:
echo "hey world" > greet.txtthengit add greet.txtthengit commit -m "Change greeting on main" - Try to merge:
git merge feature— conflict! - Resolve:
echo "hello world" > greet.txtthengit add greet.txtthengit commit -m "Resolve merge conflict"
Tips
- Don't panic — conflicts are normal and expected
- Always run
git statusduring a conflict to see what needs resolving - The merge commit message should explain how you resolved the conflict