Lesson 1: The Negotiator (Merge Conflicts)
What happens when two people edit the same line of the same file? Git can't decide which version to keep. It raises its hands and says: "You figure it out." This is a merge conflict.
When Do Conflicts Happen?
Conflicts occur when:
- Two branches modify the same line of the same file.
- One branch deletes a file that the other modified.
- Both branches add a file with the same name.
What a Conflict Looks Like
When Git encounters a conflict, it marks the file with special conflict markers:
<<<<<<< HEAD
const port = 3000;
=======
const port = 8080;
>>>>>>> feature/new-port
<<<<<<< HEAD— Your current branch's version.=======— The divider between the two versions.>>>>>>> feature/new-port— The incoming branch's version.
How to Resolve a Conflict
Step 1: Identify the conflicting files
git status
# You'll see: "both modified: app.config"
Step 2: Open the file and decide
Edit the file to keep the version you want. Remove the conflict markers:
// BEFORE (with conflict markers):
<<<<<<< HEAD
const port = 3000;
=======
const port = 8080;
>>>>>>> feature/new-port
// AFTER (your decision):
const port = 8080;
Step 3: Mark as resolved
git add app.config
Step 4: Complete the merge
git commit -m "Resolve port conflict, use 8080"
Tips for Resolving Conflicts
- 🧠 Understand both sides before choosing.
- 📞 Talk to the other developer if you're unsure.
- ✅ Run tests after resolving to make sure nothing broke.
- 🔍 Use
git diffto double-check your resolution.
booting...
Mission Objective
Experience your first merge conflict:
- Set the stage: Create a branch with
git checkout -b conflict-demo. - See the conflict: Run
cat app.configto view the conflict markers. - Resolve it: After editing, mark the file as resolved with
git add app.config.
Real-World Wisdom
Senior engineers say: "The best way to handle merge conflicts is to prevent them." Keep branches short-lived, merge frequently, and communicate with your team about which files you're editing.