Fix Git 'Merge Conflict' Error (How to Resolve)
Updated 2026-08-13

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
- Keep the version you want
- Delete the conflict markers (
<<<<,====,>>>>) - Save the file
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:
- See both versions side-by-side
- Click to choose which version to keep
- Automatically creates the merged file
Common merge tools
- VS Code (best, built-in)
- Meld
- Beyond Compare
- Kdiff3
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
- Let teammates know which files you’re changing
- Make small, focused commits
- Merge frequently instead of letting branches diverge
Keep Branches Short-Lived
- Merge back to main within 1-2 days
- Reduces divergence, fewer conflicts
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
| Scenario | Solution |
|---|---|
| Same line edited differently | Choose version or combine both |
| One branch adds, other deletes | Decide: keep or remove |
| Both add new functions | Keep both (unless they conflict) |
| Both modify variable name | Choose one name or refactor |
Checklist
- ✅ Ran
git statusto see conflicted files - ✅ Opened file and found conflict markers
- ✅ Edited to keep correct version
- ✅ Deleted all
<<<<,====,>>>>markers - ✅ Ran
git add filename - ✅ Ran
git committo complete merge - ✅ Verified merge with
git log
Related Issues
- “Detached HEAD”: use
git checkout branch-nameto fix - “Failed to push”: pull first:
git pull origin main - “Nothing to commit”: nothing changed, safe to proceed
Last updated: 2026-08-13
☕ Coffee Corner
Compiling, deploying, or waiting on a render? Here's what to brew while you wait.
🏠 Smart Home Picks
Same hobbyist care applied to your network and your front door.




