Keep Your Branch in Sync
The One Rule You Must Follow
The rule is simple: rebase before you push, merge after you push.
Why it matters
When you rebase, Git deletes your original commits and creates new ones with new IDs. This is fine when those commits only live on your machine. But once they're on the remote, your teammates can see them and build on top of them. Rewriting them pulls the floor out from under everyone else.
The safe zone - before pushing
You're working on feature/login locally. main has moved forward while you were working:
C---D feature/login (local only)
/
A---B---E main
Nobody has seen C and D yet. You can rebase freely:
git rebase main
Your commits get replayed on top of E:
C'--D' feature/login
/
A---B---E main
Clean, linear, no problem. Now you push.
The danger zone - after pushing
You pushed feature/login. Your teammate pulled it and added their own commit F:
A---B---E---C'---D'---F (teammate's local)
Now you rebase again:
git rebase main # rewrites C' and D' into C'' and D''
git push origin feature/login # Git rejects this - histories don't match anymore
git push --force origin feature/login # you force it through
Learn Git in a Day
Everything you need, nothing you don'tEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
