Force Push and Its Dangers
What you'll learn: When rewriting history requires force pushing, why it's dangerous, and how to use --force-with-lease as a safer alternative.
Why Force Push Exists
When you rewrite history (using interactive rebase, amending commits, or cherry-picking), your local branch and the remote branch diverge. Git normally rejects your push because the histories don't match—it thinks you're missing commits. To override this safety check, you need a force push.
The Danger Zone
Using git push --force is like bulldozing over what's on the remote. If a teammate pushed commits since you last pulled, you'll erase their work. It's gone from the remote, and they'll have a nightmare trying to recover it (though the reflog might save them locally).
Think of it like overwriting a shared Google Doc without checking if someone else added content while you were editing offline.
The Safer Alternative: --force-with-lease
Instead of git push --force, use:
git push --force-with-lease
This checks: "Is the remote branch still where I last saw it?" If someone else pushed in the meantime, Git refuses your push. You'll need to pull their changes first, then decide how to integrate them before pushing again.
It's like saying "I'll overwrite, but only if nobody else touched it since I last checked."
When to Force Push
- After cleaning up your feature branch with interactive rebase
- After amending commits
- Only on branches you own (not shared branches like
main)
Key Takeaway: Force pushing rewrites remote history and can destroy others' work. Always prefer --force-with-lease over --force, and only force push on branches you control.