How to Fix Git Merge Conflict Resolution Error
In this tutorial, you'll learn about How to Fix Git Merge Conflict Resolution Error. We cover key concepts, practical examples, and best practices.
You run git merge and get Automatic merge failed; fix conflicts and then commit the result โ Git cannot automatically merge changes because the same parts of the same files were modified in both branches.
The Problem
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
Step-by-Step Fix
Step 1: Identify conflicted files
git status
Expected:
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
both modified: index.html
Step 2: View the conflict markers
Open the file. You will see:
<<<<<<< HEAD
<h1>Welcome to our site</h1>
=======
<h1>Welcome to our website</h1>
>>>>>>> feature/new-header
<<<<<<< HEADโ your current branch's version=======โ separator>>>>>>> feature/new-headerโ incoming branch's version
Step 3: Resolve the conflict
Edit the file to keep the correct version:
<h1>Welcome to our website</h1>
Remove the conflict markers.
Step 4: Mark as resolved
git add index.html
Step 5: Complete the merge
git commit
Git opens an editor with a default merge message. Save and close.
Step 6: Use a merge tool
# Configure a merge tool
git config merge.tool vimdiff
# Launch merge tool
git mergetool
Step 7: Abort the merge if needed
git merge --abort
This returns to the state before the merge.
Prevention Tips
- Communicate with your team about which files you are editing
- Pull and rebase frequently to minimize divergence
- Use feature branches and merge often
- Use
git diffbefore merging to see potential conflicts - Set up a
.gitattributesfile for line ending consistency
Common Mistakes with merge conflict
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro