Sub topic 8
Pinning the expected commit explicitly
This chapter assumes you know what a remote-tracking branch such as origin/feature/login is, and that you have used git rebase or git commit --amend and then needed to force push. It builds on how the lease check works: --force-with-lease compares the real remote branch with your remote-tracking branch.
The problem with the implicit lease
A bare --force-with-lease uses your remote-tracking branch as the expected value. Git reads origin/feature/login at push time and says, "overwrite the remote branch only if it still points at this commit."
That value can change between the moment you decide to push and the moment Git reads it. Any git fetch, git pull, or editor auto-fetch moves origin/feature/login forward. The lease then agrees with the remote, even though you never looked at the new commits. The push goes through and destroys them.
Pinning the expected value removes that dependency. You name the commit yourself, so a later fetch cannot change what the lease compares against.
The syntax
The full form is:
--force-with-lease=<refname>:<expect><refname>is the branch on the remote, such asfeature/login.<expect>is the commit ID you believe the remote branch points to.
Git rejects the push if the remote branch is anywhere other than <expect>.
Worked example
Capture the commit ID when you know the state is right, which is before you start rewriting history. Then use it in the push.
expected=$(git rev-parse origin/feature/login)
git push --force-with-lease=feature/login:"$expected" origin feature/loginThe first line stores the commit ID in a shell variable. The second line passes it to the lease. Quoting "$expected" keeps the command safe if the variable is ever empty or unexpected.
The order of events matters. This is the safe sequence:
- Fetch and inspect the remote branch.
- Save its commit ID.
- Rebase or amend.
- Push with the pinned value.
If a teammate pushes between steps 2 and 4, the remote no longer matches $expected, and Git refuses.
Where the saved value comes from
The saved value is only as good as the moment you captured it. Two habits keep it meaningful:
- Capture it right after a deliberate
git fetchand after you have looked at what arrived, for example withgit log --oneline HEAD..origin/feature/login. - Do not capture it from a branch you have not inspected. Saving the ID of commits you never read gives you the same false confidence as the implicit lease.
If you want the value to survive across terminals or a long session, write it down or store it in a variable in the same shell. A value from a different day is likely out of date.
Common errors
- Omitting the colon and value.
--force-with-lease=feature/loginwith no:<expect>is valid, but it uses the remote-tracking branch for that ref. It protects only one branch, and it does not pin anything. - Using the wrong ref name. The
<refname>is the branch name on the remote, notorigin/feature/login. A mismatch means the lease does not apply to the branch you push. - Saving the ID after the fetch you did not review. The pin then records commits you have not seen.
- Expecting a fix for a missing integration. A pinned lease guards the remote's state. It does not check that your local branch contains the remote's commits. That is the job of
--force-if-includes, covered in a later chapter.
When to pin
Pin when the cost of a mistake is high or when a fetch may run behind your back: a shared branch, a long-running rewrite, or a script. For quick work on a branch only you use, the plain --force-with-lease is usually enough.
Exercise
You run git fetch, review the new commits on origin/feature/login, and save expected=$(git rev-parse origin/feature/login). You then rebase. While you work, a teammate pushes another commit to feature/login, and your editor auto-fetches it. You run git push --force-with-lease=feature/login:"$expected" origin feature/login. Does the push succeed? Would a bare --force-with-lease have succeeded?
Show answer
The pinned push is rejected, because the remote branch no longer equals the saved ID. A bare --force-with-lease would have succeeded. The auto-fetch moved origin/feature/login to the teammate's commit, so the lease would have matched the remote and overwritten the new commit without you reviewing it.
Exercise
Why is git push --force-with-lease=origin/feature/login:"$expected" origin feature/login likely to misbehave?
Show answer
The ref name should be the branch name on the remote, feature/login. Using origin/feature/login names a ref the remote does not have, so the lease does not protect the branch you are pushing.