You reworded a commit on fix/cart-rounding. The branch is already on the server, so a normal push is rejected. The tempting answer is git push --force. It works, and it also deletes whatever else is on that branch, including a commit a teammate pushed ten minutes ago.
--force-with-lease is the safer way to do the same thing. It has one blind spot, and you should know where it is before you trust it.
The examples use two clones of acme/web, yours and Ben’s, and a branch you both push to.
Why a plain force push is risky
Ben pushed Add rounding test to the branch. You didn’t fetch, so you don’t know. You amend your last commit and force push:
git push --force
To git.example.com:acme/web.git
+ 3d839ac...aee3380 fix/cart-rounding -> fix/cart-rounding (forced update)
Git didn’t ask anything. The server branch now looks like this:
git log --oneline origin/fix/cart-rounding
aee3380 Round totals to cents
cb5825a Fix cart rounding
ea171f8 Start cart
Add rounding test is gone from the branch. Ben still has it in his clone, but nobody told him, and his next push is rejected for reasons he has to work out.
What –force-with-lease checks
The lease turns “overwrite the server branch” into “overwrite it only if it is still where I last saw it”:
git push --force-with-lease
Git compares the server’s branch with your remote-tracking ref, your local copy of origin/fix/cart-rounding. If they match, the push goes through. If they differ, it stops. Run in the same situation as before, with Ben’s commit on the server and nothing fetched:
To git.example.com:acme/web.git
! [rejected] fix/cart-rounding -> fix/cart-rounding (stale info)
error: failed to push some refs to 'git.example.com:acme/web.git'
stale info means your picture of the branch is out of date. Nothing was overwritten. When nobody else has pushed, the same command succeeds:
To git.example.com:acme/web.git
+ fb3e1f9...aee3380 fix/cart-rounding -> fix/cart-rounding (forced update)
The trap: a fetch you didn’t read
The lease trusts your origin/fix/cart-rounding, and anything that updates it also updates what the lease protects. An editor, a Git GUI or a scheduled job can run a fetch in the background. So can you, with git fetch out of habit:
git fetch
From git.example.com:acme/web
fb3e1f9..3d839ac fix/cart-rounding -> origin/fix/cart-rounding
Your remote-tracking ref now points at Ben’s commit. You never looked at it, but Git treats it as seen. The same push that was rejected a minute ago now goes through:
git push --force-with-lease
To git.example.com:acme/web.git
+ 3d839ac...aee3380 fix/cart-rounding -> fix/cart-rounding (forced update)
Ben’s commit is gone again, and this time the lease approved it.
Pin the lease to the commit you reviewed
Name the commit you have actually looked at. After a fetch, read what arrived:
git log --oneline HEAD..origin/fix/cart-rounding
3d839ac Add rounding test
fb3e1f9 Round totals
The top line is Ben’s commit. The second is your own commit as it was before you reworded it, which is expected after a rewrite. Now spell out the commit you reviewed, fb3e1f9, as the one you expect to find on the server:
git push --force-with-lease=fix/cart-rounding:fb3e1f9
To git.example.com:acme/web.git
! [rejected] fix/cart-rounding -> fix/cart-rounding (stale info)
error: failed to push some refs to 'git.example.com:acme/web.git'
The server has moved past fb3e1f9, so the push stops, whatever a fetch did to your remote-tracking ref in the meantime. Read Ben’s commit, bring it into your branch with a rebase or a merge, then push again with the lease naming the tip you saw last.
Add –force-if-includes
Git 2.30 added a second check you can switch on next to the lease:
git push --force-with-lease --force-if-includes
It refuses when the remote-tracking ref holds a commit your local branch never included. In the situation above, with Ben’s commit fetched but not merged, it stops:
To git.example.com:acme/web.git
! [rejected] fix/cart-rounding -> fix/cart-rounding (remote ref updated since checkout)
error: failed to push some refs to 'git.example.com:acme/web.git'
hint: Updates were rejected because the tip of the remote-tracking branch has
hint: been updated since the last checkout. If you want to integrate the
hint: remote changes, use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
That closes the background fetch gap for ordinary pushes. Naming the commit still wins when you want to be certain, because the check works from your local history, not from what you read.
When not to force push
- Branches other people build on. The lease only protects a commit that is on the server right now. A teammate who branched from your old commits has to untangle that on their own. Ask first, or merge instead of rebasing.
main, release branches and anything that deploys. Don’t force push them, with or without a lease. Turn force pushes off on the server: see how to stop accidental Git pushes to production.- When a normal push is rejected and you don’t know why. The rejection can mean someone else pushed, not that you rewrote history. See what to do when your branch and origin have diverged, and preview what you are about to push before anything leaves your machine.
- When you haven’t rewritten anything. A plain
git pushis the right tool. A force flag is for history you changed.
Or let your Mac keep the guard
push& (Pushand) never sends a plain force push. A history rewrite is off until you turn it on, it checks the server for newer commits first, and it asks you to type the repository name. Read how push& handles safety.