The Hook (The "Byte-Sized" Intro)
Merge conflicts aren't errors — they're compliments. They mean two people cared enough about the same code to change it. Git handles thousands of merges silently, but when two developers edit the same line differently, Git pauses and says: "I can't read your minds. You decide." Understanding this turns conflicts from panic moments into 30-second decisions.
📖 What are Merge Conflicts?
A merge conflict occurs when Git cannot automatically combine changes because two branches modified the same lines in the same file differently. Git marks the conflicting sections and asks you to choose which version to keep.
Conceptual Clarity
When conflicts happen:
- Two branches change the same line(s) in the same file differently
- One branch deletes a file that the other branch modified
- Both branches add a file with the same name but different content
When conflicts do NOT happen:
- Two branches change different files — Git merges automatically
- Two branches change different parts of the same file — Git merges automatically
- One branch changes a file, the other doesn't touch it — no conflict
Conflict markers:
<<<<<<< HEAD
Your version (current branch)
=======
Their version (merging branch)
>>>>>>> feature/loginReal-Life Analogy
Two editors review the same paragraph simultaneously. Editor A changes "The car is red" to "The car is blue." Editor B changes it to "The car is green." The system can't auto-merge because both edits target the exact same sentence. It highlights both versions and says: "You two sort this out."
Visual Architecture
Why It Matters
- They're normal: Every team encounters conflicts. They're not a sign of failure.
- Git catches them: Without conflict detection, overlapping changes would silently overwrite each other.
- Reading markers is essential: Understanding
<<<<<<<,=======,>>>>>>>is a non-negotiable developer skill. - Prevention: Small, frequent merges reduce conflicts by keeping branches short-lived.
Code
# ─── Trigger a conflict (demo) ───
# On main:
echo "color = blue" > config.txt
git add config.txt && git commit -m "Set color to blue"
# Create a feature branch from earlier:
git switch -c feature/theme
echo "color = green" > config.txt
git add config.txt && git commit -m "Set color to green"
# Switch back and try to merge:
git switch main
git merge feature/theme
# Output:
# Auto-merging config.txt
# CONFLICT (content): Merge conflict in config.txt
# Automatic merge failed; fix conflicts and then commit.
# ─── See which files are conflicted ───
git status
# Output:
# Unmerged paths:
# both modified: config.txt
# ─── View the conflict markers ───
cat config.txt
# Output:
# <<<<<<< HEAD
# color = blue
# =======
# color = green
# >>>>>>> feature/themeAnatomy of a Conflict Marker
| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of YOUR changes (current branch) |
======= | Separator between the two versions |
>>>>>>> feature/theme | End of THEIR changes (merging branch) |
Your job: delete the markers, keep the correct content, then stage and commit.
Key Takeaways
- Conflicts happen when the same lines are changed differently in two branches.
- Git marks conflicts with
<<<<<<<,=======,>>>>>>>— learn to read them instantly. - Conflicts are normal and happen in every team. They're not errors.
- Short-lived branches and frequent merges reduce the frequency and size of conflicts.
Interview Prep
-
Q: What causes a merge conflict? A: A merge conflict occurs when two branches modify the same lines in the same file differently. Git cannot determine which version to keep automatically, so it marks both versions and asks the developer to resolve.
-
Q: Does Git always create conflicts when two people edit the same file? A: No. Git only creates conflicts when the same lines are changed. If two branches edit different parts of the same file, Git merges them automatically without conflict.
-
Q: How can you minimize merge conflicts? A: Keep branches short-lived, merge frequently from
main, communicate with teammates about shared files, and break large changes into smaller commits.