Resolve a merge conflict
When to use this: A merge or pull stopped with CONFLICT because the same lines were changed in two places.
git status before you run it. Before you start
Steps
-
See which files conflict
terminalgit statusLook under 'Unmerged paths' for files marked
both modified. -
Open each file and find the markers
Git inserts three marker lines. Everything between
<<<<<<< HEADand=======is YOUR version, and between=======and>>>>>>> branchis the INCOMING version.Expected output
<<<<<<< HEAD <h1>Welcome</h1> ======= <h1>Hello there</h1> >>>>>>> main
-
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. -
Mark it resolved
terminalgit add src/pages/index.astroStaging the file tells Git that you resolved it. Repeat for every conflicted file.
-
Finish the merge
terminalgit commitOpens the prepared message (
Merge branch ...). Save and close. Then test and push.
How to verify it worked
git statusis clean, andgit grep -n "<<<<<<<"finds nothing.- The site builds:
npm run build.
How to undo it
Give up and go back to before the merge started:
git merge --abortCommon errors and fixes
<<<<<<< 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.