Sub topic 12
Pushing rewritten history with --force-with-lease
This chapter covers publishing a branch after you have rewritten it. It assumes you can run git rebase -i, you know what a remote branch is, and you have used git push and git fetch.
Why a normal push fails
A rebase replaces your commits with new ones that have new hashes. If you had already pushed the old commits, the remote branch and your local branch now disagree. Neither one contains the other, so the history has diverged.
git push origin my-featureGit rejects this push. It reports that the update was rejected because the tip of your branch is behind its remote counterpart, or that the branch has diverged. The rejection protects the remote from losing commits, so it is not an error you need to work around. You have to decide to overwrite the remote history, and you should make that decision on purpose.
Why plain --force is risky
git push --force replaces the remote branch with your local one without checking anything. Suppose a teammate pushed a commit to my-feature after your last fetch. Your local branch does not contain it. A forced push discards it from the remote, and your teammate may not notice until their next pull fails.
The safer option
--force-with-lease makes the overwrite conditional. Git compares the remote branch with your remote-tracking branch, origin/my-feature, which is your last known copy of it. If they match, the push goes ahead. If someone else has pushed since your last fetch, Git refuses.
git push --force-with-lease origin my-featureThe check is only as good as your tracking ref. If you run git fetch and do not look at what arrived, your tracking ref now includes your teammate's commit. The lease check then passes, and the push still discards that commit. Fetch deliberately, and inspect new commits before you push.
A worked sequence
This sequence cleans a feature branch and publishes it.
git fetch origin
git log --oneline origin/my-feature..my-feature
git rebase -i origin/main
git push --force-with-lease origin my-feature- Fetch first, so your tracking refs are current and the lease check is meaningful.
- List the commits that exist locally but not on the remote, to confirm what you are about to change.
- Rebase interactively onto the updated base.
- Push with the lease.
If the push is refused, someone changed the remote branch. Run git fetch, then git log my-feature..origin/my-feature to see their commits. Rebase onto them or merge them in, and push again.
When not to rewrite
Do not rewrite history that others have based work on. Shared branches such as main or a release branch should keep their commits. If you rewrite a branch your teammates have pulled, their copies still contain the old commits, and the next merge brings those old commits back next to your new ones as duplicates. Rewrite your own feature branches, ideally before review starts or with the reviewers' agreement.
Checking the result
After pushing, compare the two branches to confirm they match.
git status
git log --oneline -n 5 origin/my-featuregit status should report that your branch is up to date with origin/my-feature. If you pushed the wrong thing, the reflog on your machine still holds your old tip. Recovery was covered in the previous chapters, and you can push the restored branch the same way.
Exercise
A teammate pushed a commit to origin/my-feature an hour ago. You have not fetched since. You rebase your local my-feature and run git push --force-with-lease origin my-feature. What happens, and why? What would plain git push --force have done?
Show answer
The push is refused. Your tracking ref origin/my-feature does not match the real remote branch, because it lacks your teammate's commit, so the lease check fails and nothing is overwritten. Plain --force would have replaced the remote branch without checking, and your teammate's commit would have been removed from it.
Exercise
You run git fetch, see no problem, and git push --force-with-lease succeeds. A teammate later says their commit is gone. What most likely went wrong?
Show answer
The fetch updated origin/my-feature to include the teammate's commit, so the lease check passed. Because your rebased branch did not include that commit, the push still discarded it. You need to review what a fetch brings in before pushing, and rebase onto or merge the new commits.