I squashed two commits, typed git push --force-with-lease, and felt responsible. That's the safe force push. Everyone says so. I'd even told a junior dev to alias it.
Ten minutes later a teammate asked where their commit went.
It was gone from main on the remote. My "safe" push had overwritten it, and Git didn't print a single warning. The culprit wasn't me, and it wasn't Git being buggy. It was my editor running git fetch in the background while I was rebasing.
This post shows exactly why git push --force-with-lease fails when something fetches behind your back, gives you a 30-second script that reproduces it, and shows the one flag that closes the hole.
TL;DR
-
git push --force-with-leasewith no argument compares the remote branch against your local remote-tracking ref (origin/main), not against what you actually rebased on. - Any
git fetchafter you started rewriting (editor auto-fetch, a GUI client, a script, a coding agent "checking upstream") updatesorigin/mainsilently. The lease now matches, and your push overwrites the new commits. - Fix: add
--force-if-includes(Git 2.30+), or setgit config --global push.useForceIfIncludes true. It rejects the push unless the remote tip actually appears in your local branch's reflog. - Bulletproof alternative: pin the expected SHA yourself with
--force-with-lease=main:<sha>.
What does git push --force-with-lease actually check?
--force-with-lease checks one thing: "is the branch on the remote still pointing at the commit I think it points at?" If yes, the force push goes through. If no, it's rejected with (stale info).
The catch is where "what I think" comes from. When you don't pass an explicit value, Git uses your remote-tracking ref. For main, that's refs/remotes/origin/main, the thing you see as origin/main in git log.
That ref is not a snapshot of what you based your work on. It's a cache that gets overwritten by every git fetch. Every one. Including the ones you didn't run.
So the protection boils down to this:
| Event | origin/main (local) | main (on remote) | Lease result |
|---|---|---|---|
| You start a rebase | B | B | - |
| Teammate pushes C | B | C | would reject |
| Background fetch runs | C | C | passes |
| You force push | C | your rewrite | C is gone |
The lease is supposed to answer "did anyone push since I looked?" After a background fetch, it answers "did anyone push since the last time anything on my machine looked?" Those are very different questions.
How do I reproduce the force-with-lease overwrite?
Paste this into a terminal. It uses a local bare repo as the "remote", so it touches nothing real.
#!/usr/bin/env bash
set -euo pipefail
rm -rf /tmp/lease && mkdir /tmp/lease && cd /tmp/lease
git init -q --bare -b main origin.git
# Me: two commits, pushed
git init -q -b main me && cd me
git remote add origin ../origin.git
echo one > f && git add f && git commit -qm "A"
echo two >> f && git commit -qam "B"
git push -q -u origin main
cd ..
# Teammate clones
git clone -q origin.git teammate
# I start rewriting history: squash B into A
cd me
git reset -q --soft HEAD~1
git commit -q --amend -m "A+B squashed"
cd ..
# Teammate pushes on top of B
cd teammate
echo three >> f && git commit -qam "C (teammate)"
git push -q origin main
cd ..
# My editor auto-fetches in the background
cd me
git fetch -q origin
# I push "safely"
git push --force-with-lease origin main
git log --oneline origin/main
The push prints (forced update). The log shows one commit: A+B squashed. Commit C, the teammate's work, is no longer reachable from main on the remote.
Now delete the git fetch -q origin line and run it again. The push gets rejected with (stale info), exactly like the docs promise. One fetch is the entire difference between "safe" and "silently destructive."
What runs git fetch behind my back?
More things than you'd guess. In my case it was editor auto-fetch, but anything that updates remote-tracking refs counts:
-
Editor auto-fetch. VS Code has a
git.autofetchsetting. Plenty of people turn it on so the sync badge stays fresh. Other IDEs and Git GUIs have their own periodic fetch. - Desktop Git clients. Several fetch on a timer so they can show "3 commits behind."
- Shell prompts. Some fancy prompt setups fetch to show ahead/behind counts.
-
Coding agents. If you let an agent run
git fetchto "see what changed upstream" while you're mid-rebase in the same repo, it's the same problem with extra steps. -
You, in another terminal.
git fetchupdates all remote-tracking branches, not just the one you're thinking about. Fetching to check out a coworker's PR branch also refreshesorigin/main.
None of these do anything wrong. They're doing their job. The flaw is that a plain --force-with-lease treats "my machine has seen it" as "I have integrated it."
How does --force-if-includes fix force-with-lease?
--force-if-includes adds a second check: the current tip of the remote-tracking ref must be reachable from somewhere in your local branch's reflog. In plain English: you must have actually had that commit in your branch at some point, by pulling, rebasing onto it, or merging it.
A background fetch updates origin/main, but it never touches your local main reflog. So commit C was fetched but never integrated, and the push gets rejected.
Change the last push in the script to:
git push --force-with-lease --force-if-includes origin main
You get:
! [rejected] main -> main (remote ref updated since checkout)
That's the message you wanted the first time. Now git pull --rebase, look at C, put it on top of your squash, and push again. This time the check passes, because C is in your history.
Make it permanent so you never have to remember:
git config --global push.useForceIfIncludes true
With that set, every --force-with-lease push also does the reflog check. It needs Git 2.30 or newer; check with git --version.
When does --force-if-includes still let a bad push through?
It's a big improvement, not magic. Know the gaps:
-
Your reflog can lie on your behalf. If you ran
git reset --hard origin/mainafter the fetch and then reset back to your rewrite, C was in your branch's reflog for a moment. The check passes. You "integrated" it for three seconds and then threw it away. -
It only applies to the implicit lease. If you pass an explicit expected value like
--force-with-lease=main:<sha>, Git trusts your SHA and skips the reflog check. That's fine, because you're being explicit. -
Plain
--forceignores all of this.git push -fdoesn't check leases at all. If muscle memory types-f, no config saves you. - Expired reflogs. Reflog entries expire (90 days by default for reachable entries). Not a problem for a branch you rebased this morning, but worth knowing.
What's the most bulletproof way to force push?
Pin the SHA yourself. Record what the remote looked like before you started rewriting, then pass it explicitly:
base=$(git rev-parse origin/main) # right before you rebase
git rebase -i "$base"
# ...edit, squash, reword...
git push --force-with-lease=main:"$base" origin main
Now no background process can move the goalposts. Your expected value is a shell variable, not a ref that gets rewritten by every fetch. If the remote moved off $base for any reason, the push is rejected.
It's more typing, so I use it for shared branches and the config flag for everything else.
My setup now
Three lines in my global config, and I stopped worrying about it:
git config --global push.useForceIfIncludes true
git config --global alias.pushf 'push --force-with-lease'
git config --global alias.pf '!echo "use git pushf" && false'
The last alias is mild self-defense: if I type git pf expecting a force push, it refuses and reminds me of the safe one. I also block --force to main with branch protection on the server side, because client-side habits are only as good as the tired version of me at 6 p.m.
I left editor auto-fetch on. It's genuinely useful. It just shouldn't be able to disarm my safety check, and with --force-if-includes it can't.
So why did git push --force-with-lease delete the commit?
git push --force-with-lease deleted the commit because, without an explicit value, it only checks that the remote branch still matches your local remote-tracking ref (origin/main). A background git fetch from an editor, Git client, prompt, script or coding agent updates that ref to include your teammate's new commit, so the lease matches and the force push overwrites work you never saw. Add --force-if-includes (or set push.useForceIfIncludes true, Git 2.30+) so the push is rejected unless the remote tip is in your local branch's reflog, or pin the expected SHA with --force-with-lease=main:<sha> for a check no fetch can change.
Written by the developer behind Preterview, an interview prep platform.
Top comments (1)
The sentence that should be on every Git onboarding page is the one in your table: after a background fetch, the lease answers "did anything on my machine look?" instead of "did anyone push since I looked?" — same words, completely different question. The 30-second reproduction script is what makes this land; the one-line difference (delete the fetch, get "(stale info)") turns a folk belief into a fact. And the honest caveat about the reflog lying after a reset --hard is the part most "just use --force-if-includes" posts skip. I am stealing the git pf alias — the idea that the safe path should be the path of least typing, not the most. One question: with coding agents now fetching mid-rebase, have you considered making the pin-the-SHA form the default for shared branches, or do you still trust the config flag there?