Merge Conflict
You might say
After pulling, Git stopped with a conflict. Which file do I open, and which section do I change?
When the same content was changed differently on two branches, Git cannot choose and a person must decide what to keepWhen two branches edit the exact same lines differently, Git halts the merge and inserts both versions into the file marked with conflict delimiters. A conflict is not a system error but a point where diverging intentions require human judgment; resolving it involves reviewing both edits, reconciling the desired code, removing the conflict markers, and committing the resolution.
Know first
Merge Conflict
When to use it
- Find the conflicted files and open them$ git statusboth modified: index.htmlOpen this file first, then search the conflict markers
- Keep the correct result after understanding both sidesEach side changed a part; merge into one version when both are needed<h1>Xiaoli's photography homepage</h1><p>City collection</p>
- Stage and commit only after markers are gone$ git add index.html$ git commit[main 3f9c2ab] Merge branch feature/new-navCommit only after the markers are gone
- Rerun the project to confirm both sides still workConflict resolved→Open the page→Heading correct ✓Rerun to confirm both sides' changes are still there
When NOT to use it
- Commit with any conflict marker left in the file<<<<<<< HEADMarkers left in the file break page parsing
<h1>Xiaoli's homepage</h1> - Delete a whole section without reading the other sideThe heading bug the other side just fixed gets deleted tooDeleting a whole section without reading the other side may drop needed fixes
- Abort the merge immediately instead of understanding itgit merge --abort→All changes remain unmerged
- Mix unrelated edits into the conflict resolutionChanged styles and copy while resolving the heading conflictThis commit mixes unrelated changes, hard to revert and review
Anatomy
<<<<<<< HEAD (main)
<h1>Xiaoli's homepage</h1>
=======
<h1>Xiaoli’s photography homepage</h1>
>>>>>>> feature/new-nav
Below HEAD is this line on the branch you are currently on.
This whole section belongs to your branch; keep or remove it as a unit.
The divider between the two versions; delete it with the markers.
This line from the branch being merged, possibly a fix it just made.
Marks which branch this content came from, for traceability.
Variants
Keep one side
<h1>Xiaoli’s photography homepage</h1>
After reading both sides, keep only the correct content from one
Combine both
<h1>Xiaoli’s photography homepage</h1><p>City collection</p>
When each side changed a different part and both are needed
merge --abort
git merge --abort
When the conflict is too tangled, return to the pre-merge state
Typical use cases
Terminal merge conflict message
$ git merge feature/new-nav
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
Markers inside the conflicted file
git status listing conflicted files
$ git status
You have unmerged paths.
both modified: index.html
both modified means both branches changed this file; it needs a manual decision
Commit after resolving
$ git add index.html
$ git commit
[main 3f9c2ab] Merge branch feature/new-nav
All conflict markers removed; the merge is complete
Further reading