Merge conflict
This error occurs when you merge two branches that have conflicting changes on the same lines of the same file, forcing manual resolution.
Usually happens because:
- ☑ Simultaneous edits on the same lines of code on two different branches
- ☑ File deleted on one branch and edited on another branch
- ☑ Line endings formatting mismatch modifying code blocks
🔍 Quick Checklist:
What is Merge conflict?
A merge conflict happens when Git cannot automatically reconcile differences between two commits. During a merge, rebase, or pull operation, if the same lines of a file have been changed differently in both branches, Git pauses the operation, inserts special conflict markers (<<<<<<<, =======, >>>>>>>) into the affected files, and requires you to manually resolve the differences before concluding the merge commit.
Common Causes
- Concurrent changes on the same line: Two developers changed the exact same line of a file in different ways.
- File deleted in one branch but modified in another: One branch deleted a file while another branch made edits to it.
- Divergent line endings: Cross-platform differences (LF vs CRLF) modifying entire file formats, making Git perceive them as completely conflicting.
| Cause | Frequency |
|---|---|
| Simultaneous changes on the same lines of code | ⭐⭐⭐⭐⭐ |
| File deleted on one branch and edited on another | ⭐⭐⭐ |
| Line endings formatting conflicts (LF vs CRLF) | ⭐⭐ |
Common Mistakes
- Accidentally committing files with the raw Git conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`) still written inside the code, leading to syntax compilation crashes.
- Forgetting to run `git commit` after adding (`git add`) the resolved conflict files.
How to Fix
Git Operations & Verification
Conflict markers inserted by Git to show conflicting blocks in index.js.
<<<<<<< HEAD
const port = 3000; // Your change on main branch
=======
const port = 8080; // Incoming change from feature branch
>>>>>>> feature-branchPlatform Specific Fixes
Use command-line merge tools like Meld or Vimdiff to resolve conflicts graphically.
# Set default merge tool
git config --global merge.tool vimdiff
# Open conflict resolution interface
git mergetoolBest Practices
- Pull the destination branch (`main`) into your working branch frequently to resolve small conflicts early.
- Avoid making large formatting edits (like re-indenting entire files) in shared codebase files.
Frequently Asked Questions (FAQ)
Q: What are conflict markers?
Git inserts <<<<<<< to mark the beginning of your changes (HEAD), ======= as the separator, and >>>>>>> to mark the end of incoming changes from the merged branch.
Q: How do I undo a merge conflict state?
You can safely abort the merge process and restore your previous branch state by running 'git merge --abort'.
Q: How do I resolve conflicts using a specific tool?
Configure a merge tool using 'git config --global merge.tool <tool-name>' and run 'git mergetool'.
Q: Why did a pull command trigger conflicts?
'git pull' runs 'git fetch' followed by 'git merge' under the hood. If your local changes clash with remote commits, the merge phase will trigger conflicts.