Menu
Coddy logo textTech

Git Force Push

Last updated

A normal git push is rejected when your local branch has diverged from the remote - Git won't overwrite commits you might not have seen. Force pushing overrides that check and makes the remote branch match your local one. You need it after rewriting history (a rebase or amend), but it can erase other people's commits, so reach for the safer --force-with-lease.

Try these in the terminal playground - a real shell in your browser, nothing to install.

A force push makes the remote branch match your rewritten local history - the remote's diverged commit is discarded.

Syntax

CommandWhat it does
git push --forceOverwrite the remote branch, no safety check
git push -fShort form of --force
git push --force-with-leaseForce only if no one else pushed meanwhile
git push --force-with-lease origin featureSafer force push to a named branch

--force vs --force-with-lease

Behavior--force--force-with-lease
Overwrites remoteAlwaysOnly if unchanged since your last fetch
Can erase others' commitsYesRefuses, unless a fetch refreshed the lease
Recommended for teamsNoYes

Git force push FAQ

When do I need to force push?
When you've rewritten history that's already on the remote - after a git rebase, a git commit --amend, or an interactive rebase that squashed commits. Your local branch no longer matches the remote's history, so a normal push is rejected and only a force push can update it.
What's the difference between --force and --force-with-lease?
--force overwrites the remote branch unconditionally, which can silently delete commits a teammate pushed after your last fetch. --force-with-lease only overwrites if the remote is still at the commit you last saw - if someone else pushed in the meantime, it refuses, protecting their work. One caveat: any fetch (including an IDE's background auto-fetch) refreshes the lease, so push right after you check the remote. Still prefer it over --force on any shared branch.
Is force pushing dangerous?
It can be. On a shared branch, a plain --force can overwrite commits others have pushed, effectively deleting their work from the remote. It's safe on a branch only you use. When in doubt, use --force-with-lease, which fails safely instead of clobbering unseen changes.
How do I undo a force push?
If you still have the old commits locally or in git reflog, you can reset your branch back to them and force push again to restore the previous state. This is one more reason to use --force-with-lease - it makes accidental overwrites far less likely in the first place.
Can I practice this online?
Yes. Open the terminal playground to run Git push commands in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers working with remotes step by step.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED