Resolving Merge Conflicts
Introduction
When two branches edit the same lines in the same file, Git cannot pick a winner automatically—it marks a merge conflict and waits for you. This chapter creates a conflict on purpose, reads the markers, resolves it in the editor or VS Code, and completes the merge commit.
Prerequisites
- Merging Branches
- Branches Basics
- A text editor (VS Code recommended)
Create a Conflict on Purpose
Git stops with:
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.Conflict Markers in the File
Open config.txt:
<<<<<<< HEAD
version: main
extra: main-only
=======
version: feature
>>>>>>> feature/config| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Your current branch (main) version starts |
======= | Separator |
>>>>>>> feature/config | Incoming branch version ends |
Code explanation:
- HEAD side is what you had on
mainbefore merge - Bottom side is what
feature/configchanged
Resolve Manually
Edit to the desired final content—remove all markers:
version: feature
extra: main-onlyStage and commit:
git add config.txt
git commit -m "Merge feature/config and keep both config lines"Or use default merge message Git prepared:
git commitVerify:
git log --oneline --graph
git statusShould show clean working tree.
Resolve in VS Code
When merge is in progress:
- Open conflicted file
- Click Accept Current Change, Accept Incoming, Accept Both, or Compare
- Save file
- Stage in Source Control panel
- Commit merge
Code explanation:
- VS Code reads the same
<<<<<<<markers—no special Git format
Abort If Needed
git merge --abortReturns to state before git merge—use when you picked wrong branch or need to rethink.
Reduce Conflicts in Teams
| Habit | Why |
|---|---|
| Small, frequent commits | Easier to merge |
| Pull/fetch often | Shorter divergence |
| Split files by feature | Fewer overlapping edits |
| Communicate who owns a file | Avoid parallel rewrites |
Tip
One Conflict at a Time
git status lists unmerged paths. Fix one file, git add, repeat until all resolved.
Multiple Conflicted Files
Each file gets its own markers. Resolve all, then:
git add file1 file2
git commitFAQ
Can Git auto-resolve?
Only non-overlapping edits on same file merge cleanly. Same lines = conflict.
Forgot to remove markers?
Committing <<<<<<< breaks the file—run tests and amend if not pushed.
Who resolves in a PR?
Author of the PR usually rebases or merges main into feature and fixes conflicts locally, then pushes.
git diff during conflict?
Shows combined diff; unmerged files listed under Unmerged paths.
Conflict in binary file?
Git cannot merge images/PDFs—pick one version (git checkout --ours / --theirs) or replace file manually.
Still conflicted after edit?
Ensure markers deleted and file saved, then git add.