Sub topic 6
How the lease check works
This chapter assumes you know what a remote-tracking branch is, why a rebase or amend makes a normal push fail, and how to run git push --force-with-lease (all covered earlier). Here you will see what Git compares during the lease check, and why a passing check does not prove that you have seen every commit on the remote.
Three pointers, one comparison
When you push feature/login, three different values are involved.
| Name | Where it lives | What it records |
|---|---|---|
| `feature/login` (local) | Your repository | The commit you want to publish |
| `origin/feature/login` | Your repository | The commit the remote branch pointed to the last time you fetched, pulled, or pushed |
| `feature/login` (remote) | The server | The commit the remote branch points to right now |
Your local branch is not part of the lease check. The check compares the other two. By default, --force-with-lease treats your remote-tracking branch as the value you expect the remote to have. The server's actual value must equal it.
- If they match, nobody has moved the remote branch since you last synchronized, and Git allows the overwrite.
- If they differ, someone updated the remote branch behind your back, and Git refuses.
Think of the lease as a statement: "I will overwrite this branch, but only if it is still at the commit I last saw."
The sequence during a push
Git gets the remote's current value from the list of refs the server advertises at the start of every push, so the comparison costs no extra round trip. Git then sends the update as "change this ref from the old commit to the new one". The server applies it only if the ref still holds that old commit, so a push that lands between the advertisement and your update is also caught.
Seeing the values yourself
You can reproduce the comparison by hand. Fetching first refreshes your remote-tracking branch, so run these commands in a repository where you are comfortable doing that.
git fetch origin
git rev-parse origin/feature/login
git ls-remote origin refs/heads/feature/logingit rev-parse prints the commit your remote-tracking branch records. git ls-remote asks the server for its current value of the branch. If the two hashes are identical, a --force-with-lease push of that branch passes the check. If they differ, the push is rejected.
Notice that the first command is a git fetch. Fetching moves origin/feature/login to match the server. Once it does, your expected value equals the remote's value, and the lease passes, whether or not you have looked at the new commits. This is the weakness of the default lease, and later chapters address it.
What the lease does and does not guarantee
The check answers one narrow question: has the remote ref moved since my remote-tracking ref was last updated?
It protects you in this situation:
- You fetch, then rebase your branch locally.
- A teammate pushes a new commit to
feature/login. - You push with
--force-with-lease. - Your remote-tracking branch still points to the old commit, the server now has the teammate's commit, and Git rejects the push.
It does not protect you in these situations:
- Your remote-tracking branch was refreshed after the teammate's push, but you never read or integrated their commit. The values match, so the push succeeds and discards their work.
- You push a branch whose remote-tracking branch does not reflect what you intended to overwrite. The lease trusts that record without question.
In both cases the lease is only as accurate as origin/feature/login. It says nothing about whether you have reviewed or merged the commits that the ref covers.
Recap
- The lease compares the server's current branch tip with your remote-tracking branch.
- Equal values allow the overwrite. Different values produce a
stale inforejection. - Your local branch does not take part in the comparison.
- Any fetch that updates
origin/feature/loginalso updates what the lease expects.
The next chapter shows what a stale info rejection looks like and how to recover from it.
Exercise
You run git fetch origin, then git rebase origin/main. Before you push, a teammate pushes a new commit to origin's feature/login. You do not fetch again. Does git push --force-with-lease origin feature/login succeed? Which two values does Git compare?
Show answer
The push is rejected with stale info. Git compares the server's current tip of feature/login, which includes the teammate's commit, with your remote-tracking branch origin/feature/login, which still holds the older commit from your last fetch. They differ, so the lease fails. Your local branch is not part of the comparison.
Exercise
A teammate pushed a commit to feature/login. Your editor then auto-fetched in the background, and you never looked at the commit. You rebase locally and run git push --force-with-lease origin feature/login. What happens, and why?
Show answer
The push succeeds and overwrites the teammate's commit. The auto-fetch updated origin/feature/login to the server's value, so the expected value and the actual value match. The lease only checks that the two pointers agree. It does not check that you have integrated the commits behind them.