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
<<<<<<< HEAD (main) <h1>Xiaoli's homepage</h1> ======= <h1>Xiaoli’s photography homepage</h1> >>>>>>> feature/new-nav
git status

Both branches changed the same heading line, so Git wrote both versions into the file for you to choose. Open the conflicted file, delete the unwanted version and markers, keep the final content, then git add and commit.

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 sides
    <h1>Xiaoli's photography homepage</h1>
    <p>City collection</p>
    Each side changed a part; merge into one version when both are needed
  • 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 work
    Conflict resolvedOpen the pageHeading correct ✓
    Rerun to confirm both sides' changes are still there

When NOT to use it

  • Commit with any conflict marker left in the file
    <<<<<<< HEAD
    <h1>Xiaoli's homepage</h1>
    Markers left in the file break page parsing
  • Delete a whole section without reading the other side
    The 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 it
    git merge --abortAll changes remain unmerged
  • Mix unrelated edits into the conflict resolution
    Changed 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
zsh — my-first-page
$ 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
📄 index.html⚠ 1 conflict
<<<<<<< HEAD (main)
<h1>Xiaoli's homepage</h1>
=======
<h1>Xiaoli’s photography homepage</h1>
>>>>>>> feature/new-nav (branch)
Read what each side changed before deciding which to keep
git status listing conflicted files
zsh — my-first-page
$ 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
zsh — my-first-page
$ git add index.html $ git commit [main 3f9c2ab] Merge branch feature/new-nav All conflict markers removed; the merge is complete
Further reading