Sub topic 8
Updating a remote branch with --force-with-lease
This chapter assumes you can add, commit, branch, push, and pull. It also assumes you can read git log output and know what origin/main means. You should have already rebased a branch and seen that rebase creates new commits.
Why a normal push fails after a rebase
A rebase replays your commits with new hashes. If you had already pushed the old versions, the remote branch and your local branch now disagree. Neither one contains the other, so Git treats the push as a non-fast-forward update and rejects it.
git push origin feature
# ! [rejected] feature -> feature (non-fast-forward)Git is protecting the remote history. In this case you do want to replace it, so you need to force the update. Do this only on a branch that nobody else builds on, such as your own feature branch under review.
Why plain --force is risky
git push --force overwrites the remote branch with your local one without checking what is there. Suppose a teammate pushed a commit to feature after your last fetch. A forced push deletes that commit from the remote, and you never see a warning.
--force-with-lease adds a condition. Git updates the remote branch only if it still points to the commit your remote-tracking branch (origin/feature) recorded at your last fetch. If someone else has pushed since then, the push is refused.
git push --force-with-lease origin featureA worked example
You rebase feature onto the latest main and then push.
git switch feature
git fetch origin
git rebase origin/main
git push --force-with-lease origin featureIf the push succeeds, no one had added commits to origin/feature since your fetch. If it fails with stale info, the remote has moved:
git fetch origin
git log --oneline feature..origin/featureThe log command lists commits that exist on the remote but not locally. Read them first. If they are a teammate's work, rebase onto them or talk to the author before you push again.
Gotchas
- The lease compares against your remote-tracking branch. If you run
git fetchand do not look at the result,origin/featurenow matches the remote, and a later--force-with-leasewill overwrite the new commits. Fetch, then inspect, then push. - Some editors and tools fetch in the background. That silently refreshes the lease and weakens the check.
- You can pin the expected value yourself:
git push --force-with-lease=feature:<hash> origin feature. The push succeeds only if the remote branch is exactly at<hash>. - Adding
--force-if-includesmakes the check stricter. It also requires that the remote-tracking commits are part of your local history.
Summary of the options
| Command | Overwrites unseen remote commits? | Use |
|---|---|---|
| `git push` | No, rejected | Normal updates |
| `git push --force` | Yes | Avoid |
| `git push --force-with-lease` | No, rejected | Pushing a rebased personal branch |
Exercise
You rebase feature, run git push --force-with-lease origin feature, and Git rejects it with "stale info". What happened, and what should you do next?
Show answer
The remote feature branch has a commit that your origin/feature does not record, so someone pushed after your last fetch. Run git fetch origin, then git log --oneline feature..origin/feature to see the new commits. Rebase onto them or coordinate with the author, then push again. Do not fall back to --force.