🔧 Error Fixes

Git Merge Conflict — How to Resolve It Step by Step


CONFLICT (content): Merge conflict in src/app.js
Automatic merge failed; fix conflicts and then commit the result.

Merge conflicts happen when Git can’t automatically combine changes from two branches because both modified the same lines. It’s not an error — it’s Git asking you to decide which version to keep.

What a Conflict Looks Like

When you open the conflicted file, you’ll see markers:

<<<<<<< HEAD
const port = 3000;
=======
const port = 8080;
>>>>>>> feature-branch
  • <<<<<<< HEAD — your current branch’s version
  • ======= — separator
  • >>>>>>> feature-branch — the incoming branch’s version

Step-by-Step Resolution

Step 1: See which files have conflicts

git status

Conflicted files show as “both modified.”

Step 2: Open the file and choose

You have three options:

Keep yours:

const port = 3000;

Keep theirs:

const port = 8080;

Combine both:

const port = process.env.PORT || 3000;

Delete the conflict markers (<<<<<<<, =======, >>>>>>>) completely.

Step 3: Mark as resolved and commit

git add src/app.js
git commit -m "Resolve merge conflict in app.js"

That’s it. The merge is complete.

Abort If You Want to Start Over

Changed your mind? Undo the entire merge:

git merge --abort

This puts you back to where you were before the merge.

Using VS Code to Resolve Conflicts

VS Code shows conflict markers with clickable buttons:

  • Accept Current Change — keep your version
  • Accept Incoming Change — keep their version
  • Accept Both Changes — keep both
  • Compare Changes — see side by side

This is the easiest way to resolve conflicts visually.

Common Scenarios

Conflict during git pull

git pull
# CONFLICT!

# Fix the files, then:
git add -A
git commit -m "Resolve merge conflict"
git push

Conflict during git merge

git merge feature
# CONFLICT!

# Fix the files, then:
git add -A
git commit -m "Merge feature with conflict resolution"

Conflict during git rebase

git rebase main
# CONFLICT!

# Fix the files, then:
git add -A
git rebase --continue

# Or abort:
git rebase --abort

Note: during rebase, you use --continue instead of commit.

Prevent Conflicts

  • Pull before you push: git pull --rebase before starting work
  • Keep branches short-lived: Merge frequently, don’t let branches diverge for weeks
  • Communicate with your team: If two people are editing the same file, coordinate
  • Use smaller commits: Easier to resolve conflicts in small changes than large ones

When Conflicts Get Complicated

For complex conflicts across many files:

# See a list of all conflicted files
git diff --name-only --diff-filter=U

# Use a merge tool
git mergetool

Most IDEs (VS Code, JetBrains) have built-in merge tools that show a three-way diff: yours, theirs, and the common ancestor.