🏠 Home / Hub

🔶 Git Lesson 03 — Merge & Conflicts

← Back to Git Menu  |  🏠 Hub

1. Merge — Combine Branches

# Merge feature into main
git switch main
git merge feature/login

# Types of merges:
# Fast-forward (no new commits on main since branch)
main ── A ── B
              └── C ── D (feature)
# After merge:
main ── A ── B ── C ── D  (just moves pointer — no merge commit)

# 3-way merge (both branches have new commits)
main ── A ── B ── M (merge commit)
              └── C ── D (feature)
# After merge: creates M commit with both histories
git merge --no-ff feature/login  # force merge commit (team prefers this)

2. Merge Conflicts

⚠️ Conflict = same file ရဲ့ same line ကို branches 2 ခုက ကွဲပြားအောင် edit ခဲ့တဲ့အခါ
# Conflict markers in file
<<<<<<< HEAD (current branch — main)
<h1>Welcome to Our Website</h1>
=======
<h1>Welcome!</h1>
>>>>>>> feature/redesign (incoming branch)

# How to resolve:
# 1. Open file in editor (VS Code shows it nicely)
# 2. Choose: Accept Current | Accept Incoming | Accept Both | Edit manually
# 3. Delete ALL conflict markers (<<<, ===, >>>)
# 4. Keep what you want:
<h1>Welcome to Our Website!</h1>   ← chosen version

# 5. Stage resolved files
git add index.html

# 6. Complete merge
git commit -m "merge feature/redesign — resolved heading conflict"

# Abort merge (go back to before merge)
git merge --abort

3. VS Code Conflict Resolution

# VS Code shows conflict visually with buttons:
# [Accept Current Change]    ← keep your version
# [Accept Incoming Change]   ← keep their version
# [Accept Both Changes]      ← keep both
# [Compare Changes]          ← side-by-side view

# Recommended workflow:
1. git merge feature/login
2. ↑ Conflict messages appear
3. VS Code Source Control tab → "!" files = conflicts
4. Click each file → resolve → save
5. git add .
6. git commit

# After resolving:
git log --oneline --graph
# ✅ Shows merge commit tying both histories together
💡 Prevention: small branches, frequent pulls, communicate with team what files you're editing

4. git stash — Save Work Temporarily

# Situation: need to switch branch but have uncommitted changes

# Save work without committing
git stash           # saves all tracked file changes
git stash push -m "WIP: login form validation"   # with message

# Now you can switch branches safely
git switch main
git pull
git switch feature/login

# Restore stash
git stash pop       # apply + remove from stash list
git stash apply     # apply but KEEP in stash list

# List stashes
git stash list
# stash@{0}: WIP: login form validation
# stash@{1}: On feature/login: started styling

# Apply specific stash
git stash apply stash@{1}

# Delete stash
git stash drop stash@{0}
git stash clear     # remove all stashes

5. git cherry-pick — Take Specific Commits

# Cherry-pick = take specific commit from any branch
# Useful: need one bug fix from feature branch, not whole branch

# Situation:
# main:    A ── B ── C
# feature: A ── B ── D ── E ── F (bug fix in E)

# Get commit hash
git log feature/login --oneline
# e4f5g6h fix: navbar dropdown on mobile  ← we want this!
# d3c4b5a add login page
# a1b2c3d initial login form

git switch main
git cherry-pick e4f5g6h   # apply that specific commit to main

# Cherry-pick range
git cherry-pick abc123..def456  # commits from abc123 to def456

# Conflict during cherry-pick
git cherry-pick --abort    # give up
git cherry-pick --continue # after resolving

← Git 02  |  Next: Git 04 → Remote & GitHub →

📌 Study Checklist