What is a Merge Conflict?
Understand what causes a Git merge conflict, how three-way merges work, how to read conflict markers, and the step-by-step process to resolve one.
Expected Interview Answer
A merge conflict happens when Git tries to combine two branches that changed the same lines (or one branch deleted a file the other edited) and cannot automatically decide which version is correct, forcing a human to choose or combine the changes manually.
Git's merge algorithm uses a three-way comparison between the common ancestor commit and the tips of both branches; when a hunk of a file was changed differently on both sides relative to that ancestor, it can't safely pick a winner and marks the file as conflicted. Git inserts conflict markers, `<<<<<<<`, `=======`, and `>>>>>>>`, directly into the file to show both versions side by side, and pauses the merge with the repository in a special conflicted state. You resolve it by editing the file to the correct final content, removing the markers, staging the file with `git add`, and completing the merge with `git commit` (or `git merge --continue` for a rebase). Conflicts aren't a sign something went wrong; they're Git correctly refusing to guess when two people legitimately changed the same code differently.
- Prevents silent, incorrect automatic overwrites of code
- Forces a human decision exactly where logic genuinely diverged
- Conflict markers pinpoint the exact lines in dispute
- Tools like `git mergetool` and IDE integrations simplify resolution
- Small, frequent merges keep conflicts small and manageable
AI Mentor Explanation
A merge conflict is like two umpires giving contradictory decisions on the same delivery after reviewing different camera angles, so the third umpire cannot simply average the calls. Play stops until someone reviews both angles side by side and rules definitively on which decision actually stands.
Step-by-Step Explanation
Step 1
The merge is attempted
`git merge feature-x` compares the common ancestor with both branch tips and applies non-overlapping changes automatically.
Step 2
Git detects overlapping changes
When the same lines were modified differently on both sides (or one side deleted what the other edited), Git can't resolve it automatically and marks the file conflicted.
Step 3
Conflict markers appear
Git inserts `<<<<<<< HEAD`, `=======`, and `>>>>>>> feature-x` around the disputed section, showing both versions in the file.
Step 4
You edit and resolve
Manually edit the file to the correct final content, deciding to keep one side, the other, or a combination, then delete the conflict markers.
Step 5
Stage and finish
`git add <file>` marks it resolved, then `git commit` (merge) or `git rebase --continue` (rebase) completes the operation.
What Interviewer Expects
- Explains conflicts arise from a three-way comparison against the common ancestor
- Knows exactly when a conflict occurs, overlapping edits, not just any concurrent change
- Can read and correctly resolve conflict markers
- Understands the required staging and commit/continue step after resolving
- Treats conflicts as normal, not as a sign of a broken repository
Common Mistakes
- Panicking and running `git merge --abort` instead of resolving the conflict
- Leaving conflict markers in the committed file by mistake
- Blindly accepting 'theirs' or 'ours' without reading the actual logic difference
- Not staging the resolved file before committing, so Git still reports a conflict
- Believing every simultaneous edit causes a conflict, even non-overlapping ones
Best Answer (HR Friendly)
“A merge conflict happens when two people change the exact same part of a file in different ways, and the system can't automatically decide which version to keep. Someone has to look at both versions and manually choose or combine them before the changes can be joined together.”
Code Example
$ git merge feature/pricing
Auto-merging src/pricing.ts
CONFLICT (content): Merge conflict in src/pricing.ts
Automatic merge failed; fix conflicts and then commit the result.
$ cat src/pricing.ts
<<<<<<< HEAD
const DISCOUNT = 0.10;
=======
const DISCOUNT = 0.15;
>>>>>>> feature/pricing
# Manually resolve, then:
$ git add src/pricing.ts
$ git commit -m "Merge feature/pricing, resolve discount conflict"
[main 9c8b7a6] Merge feature/pricing, resolve discount conflictFollow-up Questions
- How does Git's three-way merge algorithm decide what conflicts?
- What tools can help resolve conflicts visually, like `git mergetool`?
- How do conflicts differ during a rebase versus a merge?
- What does `git status` show while a conflict is unresolved?
- How can frequent, small merges reduce the size of future conflicts?
MCQ Practice
1. When does Git report a merge conflict?
Git can auto-merge non-overlapping changes fine; a true conflict only occurs when the same region diverged differently on both sides relative to the common ancestor.
2. What do `<<<<<<<`, `=======`, and `>>>>>>>` represent in a conflicted file?
Git inserts these markers to show both conflicting versions of the disputed section so a human can choose or combine them.
3. After manually resolving a conflicted file, what must you do next?
You must stage the resolved file with `git add` to mark it as resolved, then finish with `git commit` or `git rebase --continue`.
Flash Cards
What causes a merge conflict? — The same lines were changed differently on both branches relative to their common ancestor, so Git can't auto-resolve it.
What do conflict markers look like? — `<<<<<<< HEAD` ... `=======` ... `>>>>>>> branch-name`, wrapping both conflicting versions.
How do you tell Git a conflict is resolved? — Edit the file to remove markers and pick the final content, then `git add` the file.
How do you finish a merge after resolving all conflicts? — Run `git commit` (for a merge) or `git rebase --continue` (for a rebase).