Resolving Conflicts
Resolving Conflicts
Section titled “Resolving Conflicts”🤔 Why Do Conflicts Happen?
Section titled “🤔 Why Do Conflicts Happen?”A conflict occurs when two branches modify the same line in the same file differently. Git doesn’t know which change to keep.
Think of it like two people editing the same sentence in a document:
- You write: “The color is blue.”
- Your colleague writes: “The color is red.”
- Git asks: Which one is correct?
flowchart TB Start[You pull latest main] --> Conflict[Merge conflict detected!] Conflict --> Check["git status<br/>Shows conflicted files"] Check --> Edit["Open the file<br/>Look for conflict markers"] Edit --> Decide["Choose one side<br/>or write a new version"] Decide --> Add["git add <file><br/>Mark as resolved"] Add --> Commit["git commit<br/>(no message needed)"] Commit --> Done[✅ Conflict resolved!]
style Start fill:#3b82f6,color:#fff style Conflict fill:#ef4444,color:#fff style Done fill:#10b981,color:#fff🔍 What a Conflict Looks Like
Section titled “🔍 What a Conflict Looks Like”When a conflict happens, Git puts conflict markers in the file:
<<<<<<< HEADThe color is blue.=======The color is red.>>>>>>> feature-branch| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of your version (current branch) |
======= | Separator between the two versions |
>>>>>>> feature-branch | End of their version (incoming branch) |
🛠️ Step-by-Step Fix
Section titled “🛠️ Step-by-Step Fix”1. See which files have conflicts:
git status# both modified: index.html2. Open the conflicted file in your editor:
code index.html # VS Code3. Find the conflict markers and fix them:
<<<<<<< HEADThe color is blue.=======The color is red.>>>>>>> feature-branch4. Decide what to keep — one side, both, or something new:
The color is purple. ← You wrote a completely new version5. Save the file, then mark it as resolved:
git add index.htmlgit commit # Git provides a default merge message🧪 Practice Creating a Conflict
Section titled “🧪 Practice Creating a Conflict”# 1. Setupgit initecho "Hello" > file.txtgit add . && git commit -m "Initial"
# 2. Branch and commitgit checkout -b featureecho "Feature content" > file.txtgit add . && git commit -m "Feature change"
# 3. Back to main, make a different changegit checkout mainecho "Main content" > file.txtgit add . && git commit -m "Main change"
# 4. Try to merge — conflict!git merge feature# Auto-merging file.txt# CONFLICT: content in file.txtThe conflicted file.txt will look like:
<<<<<<< HEADMain content=======Feature content>>>>>>> featureFix it, then git add file.txt and git commit.
💡 Tips for Avoiding Conflicts
Section titled “💡 Tips for Avoiding Conflicts”- Pull often —
git pullbefore starting work - Communicate — let teammates know which files you’re editing
- Small commits — easier to see what changed
- Short-lived branches — merge or rebase frequently to stay in sync
- Use VS Code — it has built-in conflict resolution UI (Accept Current / Accept Incoming / Accept Both)
In Simple Words
Section titled “In Simple Words”- Conflicts happen when two branches change the same line differently
- Git marks conflicts with
<<<<<<<,=======, and>>>>>>> - Fix: Open the file → edit the content → remove markers → save
- Mark resolved:
git add <file>→git commit - Conflicts are normal! Pull often and branch frequently to minimize them