Sub topic 7
When the lease fails: stale info and how to recover
This chapter assumes you know how to commit, branch, push, and rebase. It also assumes you know what origin is and that origin/feature/login is a remote-tracking branch, a local record of where the remote branch was at your last fetch.
What a stale info rejection means
--force-with-lease overwrites a remote branch only if the remote branch still points where your remote-tracking branch says it does. When the two disagree, Git refuses the push and prints this:
To github.com:example/repo.git
! [rejected] feature/login -> feature/login (stale info)
error: failed to push some refs to 'github.com:example/repo.git'The words stale info mean your local information about the remote is out of date. Someone else, or another clone of yours, pushed a commit to feature/login after your last fetch. A plain --force would have deleted that commit from the remote branch. The lease stopped the push before that happened.
The rejection is the feature working as designed. It does not mean something is broken.
Recovering step by step
Do not respond to the error by switching to --force. That removes the protection and discards the commits you have not seen. Instead, follow these steps.
- Fetch the current state of the remote.
- Look at the commits you were missing.
- Integrate them into your branch.
- Push again with the lease.
git fetch origin
git log --oneline HEAD..origin/feature/login
git rebase origin/feature/login
git push --force-with-lease origin feature/loginThe git log --oneline HEAD..origin/feature/login command lists commits that are on the remote branch but not in your local branch. These are the commits the lease protected. Read them before you continue. They may be a teammate's fix, a review suggestion applied through the web interface, or a commit you made from another machine.
The git rebase origin/feature/login command replays your local commits on top of the remote branch, so the new commits are kept. After the rebase, origin/feature/login is an ancestor of your branch, and the lease check passes because your fetch just refreshed the remote-tracking branch.
If the missing commits conflict with your work, resolve the conflicts as you would in any rebase. If the commits turn out to be unwanted, discuss that with their author before you drop them.
A common sequence that triggers the rejection
The usual case looks like this.
git switch feature/login
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature/loginYou rebased onto the latest main, so the push is a non-fast-forward and needs a force. If a teammate pushed to feature/login between your last fetch of that branch and your push, the lease fails. Note that git fetch origin in this sequence updates origin/feature/login too. If the teammate pushed before that fetch, your rebase of local commits onto origin/main would leave their commit out of your branch, yet the lease would pass. Chapter material on background fetches covers that gap. For now, the rule is to inspect HEAD..origin/feature/login after every fetch, before you rebase and push.
Common mistakes
- Retrying with
--forceafter the error. This overwrites exactly the commits the lease found. - Running
git fetchand then pushing without reading the log. The fetch makes the next lease pass, but the new commits are still missing from your branch. - Running
git pulland accepting a merge. This integrates the commits but creates a merge commit inside a branch you are trying to keep linear. Rebasing onto the remote branch keeps the history straight. - Assuming the rejection means a network problem.
stale infois specific to the lease check. Network errors read differently.
Exercise
You run git push --force-with-lease origin feature/login and get ! [rejected] feature/login -> feature/login (stale info). What should you do next, and why not use --force?
Show answer
Run git fetch origin, then git log --oneline HEAD..origin/feature/login to see the commits you were missing. Integrate them, for example with git rebase origin/feature/login, and push again with --force-with-lease. Using --force would overwrite the remote branch and discard those commits, which is what the lease was preventing.
Exercise
What does the output of git log --oneline HEAD..origin/feature/login show, and what does an empty output tell you?
Show answer
It lists commits reachable from the remote-tracking branch but not from your local branch. Empty output means the remote branch has no commits your branch lacks, so a force push will not discard anyone's work, as far as your last fetch knows.