Resolve a merge conflict

careful intermediate #conflict#merge#markers

When to use this: A merge or pull stopped with CONFLICT because the same lines were changed in two places.

Be careful with this one. Check git status before you run it.

Before you start

Steps

  1. See which files conflict

    terminal
    git status

    Look under 'Unmerged paths' for files marked both modified.

  2. Open each file and find the markers

    Git inserts three marker lines. Everything between <<<<<<< HEAD and ======= is YOUR version, and between ======= and >>>>>>> branch is the INCOMING version.

    Expected output
    <<<<<<< HEAD
    <h1>Welcome</h1>
    =======
    <h1>Hello there</h1>
    >>>>>>> main
  3. Edit to the final text

    Keep one side, or combine them. Delete all three marker lines (<<<<<<<, =======, >>>>>>>). VS Code shows buttons: Accept Current, Accept Incoming, Accept Both.

  4. Mark it resolved

    terminal
    git add src/pages/index.astro

    Staging the file tells Git that you resolved it. Repeat for every conflicted file.

  5. Finish the merge

    terminal
    git commit

    Opens the prepared message (Merge branch ...). Save and close. Then test and push.

How to verify it worked

  • git status is clean, and git grep -n "<<<<<<<" finds nothing.
  • The site builds: npm run build.

How to undo it

Give up and go back to before the merge started:

terminal
git merge --abort

Common errors and fixes

Markers were committed by accident (<<<<<<< visible on the site) Fix the file, commit, and push again. Search the project for <<<<<<< before every push when you resolved conflicts.
You have not concluded your merge (MERGE_HEAD exists) A merge is still open. Finish it (git add + git commit) or cancel it (git merge --abort).

Q&A

Why do conflicts happen?

Two branches changed the same lines (or one edited a file the other deleted). Git cannot guess which is right, so it asks you.

How do I avoid them?

Pull often, keep branches short-lived, and merge main into your branch regularly.

Practice these as flashcards →

Terms in this scenario

Related scenarios

My notes

Saved in this browser only.