Understanding and Resolving Merge Conflicts
12 min read
When Conflicts Happen
A merge conflict occurs when the same lines of the same file were changed differently on both branches. Git cannot decide which version is correct, so it pauses the merge and marks the conflicting sections.
Reading Conflict Markers
text
<<<<<<< HEAD
const greeting = "Hello, world";
=======
const greeting = "Hi there!";
>>>>>>> feature/login<<<<<<< HEAD— start of YOUR current branch's version.=======— divider between the two conflicting versions.>>>>>>> feature/login— end of the INCOMING branch's version.
Resolving a Conflict Step by Step
- Run
git statusto see which files are conflicted (markedUU). - Open each conflicted file and decide what the final content should be — keep one version, the other, or combine them.
- Delete all conflict markers (
<<<<<<<,=======,>>>>>>>). - Stage the resolved file with
git add <file>. - Once all conflicts are resolved,
git committo finalise the merge.
bash
git status # see conflicted files
# edit files to resolve
git add src/utils.ts
git commit -m 'merge feature/login into main'VS Code's built-in merge editor highlights conflicts visually and provides one-click "Accept Current", "Accept Incoming", or "Accept Both" buttons. Use it — it is much faster than editing raw markers.
If you get stuck in a conflict and want to start over, run git merge --abort to restore your branch to the state before the merge began.
Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises