GIT: Conflict Resolve 3 Ways


๐Ÿง  Problem Scenario:

User A and User B both change the same file
User B pushes to main first ✅
User A did not pull latest main before pushing ❌
❗️Conflict occurs when A tries to push

๐Ÿ› ️ Way 1: Manual Merge (Safe and Clear)

✅ Pull latest changes from main : git pull origin main
๐Ÿง  Manually resolve conflicts in files (look for <<<<<<< , ======= , >>>>>>> )
✅ Check status: git status
✅ Stage resolved files: git add .
✅ Commit the merge : commit -m "Resolved merge conflict manually"
✅ Push to your branch: git push origin your-branch-name

๐Ÿงน Way 2: Discard Your Changes & Start Fresh
Steps:
❌ Discard current changes (danger: will delete uncommitted changes!)
git reset --hard HEAD

2.✅ Switch to main and pull latest:
git checkout main,
git pull origin main

3.✅ Switch back to your branch, re-do your changes:
git checkout your-branch-name

Make your changes again git status git add . git commit -m "Fresh changes after pulling main" git push origin your-branch-name

๐Ÿ“ฆ Way 3: Use git stash (Save & Restore)
Steps:
๐Ÿ“ฆ Save your current work:
git stash

✅ Get latest main:
git checkout main git pull origin main

๐Ÿง‘‍๐Ÿ’ป Switch back and restore changes:
git checkout your-branch-name git stash pop

✅ Add, commit, and push
git add . git commit -m "Reapplied my changes on updated main" git push origin your-branch-name

Merge a Branch into main

git checkout main
git pull origin main
git merge feature-branch
git push origin main


Why we need three way 
Method Best For Pros Cons
Way 1 (Manual) Team merge situations Full control, preserves both sides Time-consuming
Way 2 (Reset) Quick cleanup or minor changes Clean start, simple Destroys uncommitted work
Way 3 (Stash) Smart sync without manual merge Saves time, keeps changes May still conflict if large change

Comments

Popular posts from this blog

Spread and Rest

Shallow Copy and Deep Copy in JavaScript