Fix Git 'Merge Conflict' Error (How to Resolve)

Updated 2026-08-13

Programmer coding laptop
Photo: Crew crew / Wikimedia Commons (CC0)

The Error

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

What It Means

Two branches changed the same lines in a file, and Git can’t automatically decide which version to keep.

Quick Fix (3 Steps)

Step 1: See Which Files Have Conflicts

git status

Output shows files with conflicts (marked as “both modified” or “both added”).

Step 2: Open the Conflicted File

The file contains conflict markers:

<<<<<<< HEAD
This is from your current branch
=======
This is from the incoming branch
>>>>>>> feature-branch

Edit the file and

Example - before

function greet() {
<<<<<<< HEAD
  return "Hello, World!";
=======
  return "Hi there!";
>>>>>>> feature-branch
}

After (keep first version)

function greet() {
  return "Hello, World!";
}

Step 3: Complete the Merge

git add filename.js
git commit -m "Merge feature-branch into main (resolved conflicts)"

Done! Merge is complete.

Method 2: Use Git Diff to See Conflicts Clearly

git diff

This shows exactly what changed in each version, making it easier to decide.

Method 3: Accept Their Changes or Ours

If you want to keep YOUR version entirely

git checkout --ours filename.js
git add filename.js

If you want to keep THEIR version entirely

git checkout --theirs filename.js
git add filename.js

Then commit:

git commit -m "Merge resolved: kept [ours|theirs] version"

Method 4: Use Visual Merge Tool (Easier)

git mergetool

This opens a visual editor where you can:

Common merge tools

Step-by-Step: Full Merge Conflict Workflow

Scenario: You’re on main and trying to merge feature-branch

# Start the merge
git merge feature-branch

# Git says: "CONFLICT (content): Merge conflict in app.js"

# Check status
git status

# Open app.js in editor
# Find <<<<<<< HEAD ... ======= ... >>>>>>> markers
# Edit: keep the version you want, delete markers
# Save file

# Mark as resolved
git add app.js

# Complete merge
git commit -m "Merge feature-branch into main"

# Verify
git log --oneline

Prevent Merge Conflicts

Pull Latest Before Starting

git checkout main
git pull origin main

Communicate with Team

Keep Branches Short-Lived

Rebase Instead of Merge (Advanced)

git rebase main
# Then merge with fast-forward

Abort a Merge (If You Mess Up)

git merge --abort

This cancels the merge and resets to before you ran git merge.

Common Conflict Scenarios

ScenarioSolution
Same line edited differentlyChoose version or combine both
One branch adds, other deletesDecide: keep or remove
Both add new functionsKeep both (unless they conflict)
Both modify variable nameChoose one name or refactor

Checklist

Last updated: 2026-08-13