Sub topic 12
Force pushing safely with --force-with-lease
This chapter assumes you can commit, branch, and rebase locally, and that you know what a remote-tracking branch such as origin/feature/login is. You should also know why a normal push gets rejected when the remote has commits you lack.
Why rewriting history needs a force push
Rebasing, amending, and squashing create new commits to replace old ones. If you already pushed the old commits, your local branch and the remote branch no longer share a simple line of history. A normal git push is rejected, because Git only allows a push that moves the remote branch forward along the same line.
In that case the rejection is correct. You really do want the remote branch to point at your rewritten commits. The question is how to overwrite it without destroying work you have not seen.
The problem with --force
git push --force overwrites the remote branch with your local branch, whatever the remote currently holds. Suppose a teammate pushed a commit to feature/login after your last fetch. Your local copy does not contain it. A plain force push discards it from the branch, with no warning.
What --force-with-lease does
--force-with-lease makes the force conditional. Git compares the remote branch's current position with your remote-tracking branch (origin/feature/login). That is the position you last saw.
- If they match, nobody has pushed since your last fetch, and Git overwrites the branch.
- If they differ, Git refuses with a "stale info" error, and nothing changes on the remote.
git rebase origin/main
git push --force-with-lease origin feature/loginA worked example
You rebase feature/login onto the latest main, then push.
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature/loginIf it succeeds, you are done. If it fails with ! [rejected] feature/login -> feature/login (stale info), someone else pushed in the meantime. Fetch again and look at what they added.
git fetch origin
git log --oneline feature/login..origin/feature/loginThen rebase onto the updated remote branch, which includes their commits, and retry the push.
git rebase origin/feature/login
git push --force-with-lease origin feature/loginCommon errors and gotchas
- Fetching right before the push defeats the check. The lease compares against your remote-tracking branch. If a background tool or a
git fetchrun just before the push updatesorigin/feature/loginto include a teammate's commit, Git sees a match and the push goes through. Your local branch still lacks that commit, so the push erases it. Inspect what a fetch brought in before you force push. - Do not combine it with
--force. If both flags appear, the plain force wins and the protection is gone. Check shell aliases for this. - Name the branch. Writing
git push --force-with-lease origin feature/loginis clearer than relying on defaults. Without a branch name, Git may push every branch that matches the push configuration. - Shared branches stay off limits. Never force push
mainor any branch other people base work on. Hosting services can block this with branch protection.
Summary
Use --force-with-lease whenever you rewrite a branch you already published. It costs nothing extra and turns a silent overwrite into a visible error.
Exercise
You rebase your published branch feature/login, then run git push --force-with-lease origin feature/login. Git reports "stale info". What happened, and what should you do next?
Show answer
The remote branch has a commit that your origin/feature/login does not include, so someone pushed after your last fetch. Nothing was overwritten. Run git fetch origin, inspect the new commits with git log --oneline feature/login..origin/feature/login, rebase your work onto origin/feature/login, and push with --force-with-lease again.
Exercise
A teammate pushes a commit to feature/login. You run git fetch, then immediately run git push --force-with-lease origin feature/login after a rebase that did not include their commit. Why can this succeed, and why is that a problem?
Show answer
The fetch moved origin/feature/login to the teammate's commit, so the lease check sees the remote matching what you last saw. The push then overwrites the branch with your history, which lacks their commit. Always inspect fetched changes and integrate them before pushing.