Resolving Merge Conflicts

Introduction

When two branches edit the same lines in the same file, Git cannot pick a winner automatically—it marks a merge conflict and waits for you. This chapter creates a conflict on purpose, reads the markers, resolves it in the editor or VS Code, and completes the merge commit.

Prerequisites

Create a Conflict on Purpose

Git stops with:

text
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.

Conflict Markers in the File

Open config.txt:

text
<<<<<<< HEAD
version: main
extra: main-only
=======
version: feature
>>>>>>> feature/config
MarkerMeaning
<<<<<<< HEADYour current branch (main) version starts
=======Separator
>>>>>>> feature/configIncoming branch version ends

Code explanation:

  • HEAD side is what you had on main before merge
  • Bottom side is what feature/config changed

Resolve Manually

Edit to the desired final content—remove all markers:

text
version: feature
extra: main-only

Stage and commit:

bash
git add config.txt
git commit -m "Merge feature/config and keep both config lines"

Or use default merge message Git prepared:

bash
git commit

Verify:

bash
git log --oneline --graph
git status

Should show clean working tree.

Resolve in VS Code

When merge is in progress:

  1. Open conflicted file
  2. Click Accept Current Change, Accept Incoming, Accept Both, or Compare
  3. Save file
  4. Stage in Source Control panel
  5. Commit merge

Code explanation:

  • VS Code reads the same <<<<<<< markers—no special Git format

Abort If Needed

bash
git merge --abort

Returns to state before git merge—use when you picked wrong branch or need to rethink.

Reduce Conflicts in Teams

HabitWhy
Small, frequent commitsEasier to merge
Pull/fetch oftenShorter divergence
Split files by featureFewer overlapping edits
Communicate who owns a fileAvoid parallel rewrites

Tip

One Conflict at a Time

git status lists unmerged paths. Fix one file, git add, repeat until all resolved.

Multiple Conflicted Files

Each file gets its own markers. Resolve all, then:

bash
git add file1 file2
git commit

FAQ

Can Git auto-resolve?

Only non-overlapping edits on same file merge cleanly. Same lines = conflict.

Forgot to remove markers?

Committing <<<<<<< breaks the file—run tests and amend if not pushed.

Who resolves in a PR?

Author of the PR usually rebases or merges main into feature and fixes conflicts locally, then pushes.

git diff during conflict?

Shows combined diff; unmerged files listed under Unmerged paths.

Conflict in binary file?

Git cannot merge images/PDFs—pick one version (git checkout --ours / --theirs) or replace file manually.

Still conflicted after edit?

Ensure markers deleted and file saved, then git add.