Sub topic 9
The pitfall: fetches that silently refresh your lease
This chapter assumes you know what a remote-tracking branch is, how git push --force-with-lease compares it to the remote, and what a stale info rejection looks like. Earlier chapters covered those. Here you will see how the lease can pass when it should not.
What the lease actually trusts
The lease check compares two values:
- The commit the remote branch points to right now.
- The commit your remote-tracking branch (
origin/feature/login) records.
If they match, Git assumes you have seen the remote's current state and allows the overwrite. The check does not ask whether you read or merged those commits. It only asks whether your local copy of the remote's position is up to date.
That is the weak point. Anything that updates origin/feature/login without you looking at the new commits makes the two values match again.
How a background fetch defeats the lease
git fetch moves remote-tracking branches. Many tools run it for you:
- IDEs such as VS Code and JetBrains products, which auto-fetch every few minutes.
- Git GUI clients.
- Shell prompt plugins and scheduled jobs that refresh status.
Suppose a teammate pushes a commit to feature/login while you are rebasing. Your IDE fetches in the background. Now origin/feature/login includes their commit, but your local branch does not. When you run git push --force-with-lease, the remote and your remote-tracking branch agree, so the push succeeds. Your rebased history replaces the branch, and your teammate's commit is gone from it.
You never saw an error, and you never saw the commit. The lease worked as designed, but the protection you wanted was gone.
Spotting the situation
Before a forced push, compare your branch with its remote-tracking branch:
git fetch origin
git log --oneline HEAD..origin/feature/loginIf this prints commits, the remote has work your local branch does not contain. After a rebase of your own commits you may also see your old, pre-rebase commits listed here. That is expected. Look for commits you did not write.
To check authorship, add the author name:
git log --format='%h %an %s' HEAD..origin/feature/loginAny commit by someone else needs to be integrated first, as described in the recovery steps from the previous chapter.
Defense 1: pin the expected commit
You can tell Git which commit you expect on the remote, so a fetch cannot change the comparison:
expected=$(git rev-parse origin/feature/login)
git push --force-with-lease=feature/login:"$expected" origin feature/loginThe value must be captured at a moment when you know it is safe, ideally right after you last reviewed the remote history. Record it before the IDE has a chance to fetch, or copy a commit hash from your own review. If you compute it just before the push, as above, you re-introduce the problem, because origin/feature/login may already be refreshed. Pinning helps only when the hash comes from a point you trust.
Defense 2: add --force-if-includes
--force-if-includes adds a second condition. Git checks that the latest commit of the remote-tracking branch is reachable from your local branch, using your branch's reflog. In practice, it confirms you integrated what you fetched.
git push --force-with-lease --force-if-includes origin feature/loginIn the scenario above, origin/feature/login points to the teammate's commit C5. Your rebased branch does not contain C5, so the push is rejected. The background fetch no longer hides the missing commit.
Notes on this option:
- It needs a Git version that supports it (2.30 or later).
- It only works together with
--force-with-leaseand with no explicit expected value. - It relies on the reflog, so it is weaker if the reflog was expired or the branch was recreated.
Choosing between them
| Approach | Protects against background fetch | Effort |
|---|---|---|
| `--force-with-lease` alone | No | None |
| `--force-with-lease=<ref>:<expect>` | Yes, if the hash is trusted | Manual hash tracking |
| `--force-with-lease --force-if-includes` | Yes | One extra flag |
For daily work, the extra flag is the simpler habit. Use the pinned form in scripts or when you want an exact, auditable expectation.
Common errors
- Assuming
--force-with-leasemeans "no one pushed since I started." It means "the remote matches my last known copy." - Fixing a
stale inforejection by fetching and pushing again without reading the new commits. The fetch makes the retry pass, but nothing was integrated. - Leaving IDE auto-fetch on and never checking
git log HEAD..origin/<branch>before a forced push.
Exercise
You rebased feature/login locally. Your IDE auto-fetched, and now git push --force-with-lease origin feature/login succeeds, yet a teammate's commit disappears from the branch. Why did the lease not stop it, and which option would have?
Show answer
The auto-fetch updated origin/feature/login to include the teammate's commit, so the remote and the remote-tracking branch matched and the lease passed. Adding --force-if-includes would have rejected the push, because the remote-tracking branch's tip was not part of your local branch. Pinning an earlier trusted hash with --force-with-lease=feature/login:<hash> would also have failed the check.
Exercise
Why is expected=$(git rev-parse origin/feature/login) immediately before the push a weak way to pin the expected value?
Show answer
It reads the current remote-tracking ref, which a background fetch may already have advanced. The lease then compares the remote against that same refreshed value and passes, so the pin gives no protection. The hash should come from a point where you knowingly reviewed the remote branch.