Sub topic 11
Publishing a Rebased Branch with --force-with-lease
This chapter assumes you can run git rebase --onto, you know that a rebase copies commits instead of moving them, and you have pushed a branch to a remote such as origin. It covers how to publish the rewritten branch without overwriting other people's work.
Why a normal push fails
A rebase gives your commits new IDs. The old commits still exist on the remote branch, but your local branch no longer contains them. Git sees the two histories as diverged, and a plain git push is rejected as a non-fast-forward update.
! [rejected] topic -> topic (non-fast-forward)The only way to publish the new history is to tell the remote to move its branch to a commit that is not a descendant of its current tip. That is a force push.
Why not plain --force
git push --force replaces the remote branch with your local one without checking anything. If a teammate pushed a commit to topic after your last fetch, --force deletes it from the branch, and neither of you gets a warning.
--force-with-lease adds a condition. Git compares the remote's current value for the branch with your remote-tracking branch (origin/topic). If they match, nothing has changed since you last fetched, and the push goes through. If they differ, Git refuses.
git push --force-with-lease origin topicA complete publishing workflow
This example reuses the earlier --onto move. It previews the range, rebases, checks the result, and then pushes.
git fetch origin
git log --oneline next..topic
git rebase --onto origin/main next topic
git log --oneline --graph --decorate origin/main topic
git push --force-with-lease origin topicThe git fetch at the start matters. It updates origin/topic, which is the value the lease checks. If someone else pushed to topic, you see their commits before you rebase, and you can include them in the rebase instead of discarding them.
Where the lease can mislead you
The lease compares against your remote-tracking branch, not against what you have reviewed. Suppose you run git fetch and a teammate's new commit arrives in origin/topic. You do not look at it and you rebase without it. Because origin/topic now matches the remote, the lease passes and the push deletes their commit.
There are two ways to tighten this.
- Name the expected value yourself. The push fails if the remote tip is anything other than that commit.
git push --force-with-lease=topic:3f2a9c1 origin topic- Add
--force-if-includes(Git 2.30 or later). It also requires that the remote-tracking tip is something your local branch has already integrated, as recorded in your reflog.
git push --force-with-lease --force-if-includes origin topicWhen rewriting is not appropriate
A lease protects against races. It does not make rewriting history safe for everyone.
- Rebase and force-push only branches you own, or branches your team has agreed to rewrite. Anyone who has based work on the old commits must rebase their own work onto the new ones.
- Never force-push
mainor other shared integration branches. Many hosts block this with branch protection, and a rejected push there is intended behavior. - If a review is in progress on a pull request, tell the reviewers. Force-pushing replaces the commits they were reading.
- Use
git push --force-with-leasewith an explicit remote and branch name. With no arguments, a push may update every branch that matches your push configuration.
If the push goes wrong, the pre-rebase tip is still in your reflog. See the recovery chapter for how to restore it.
Quick reference
| Command | Overwrites unseen remote commits? |
|---|---|
| `git push --force` | Yes, always |
| `git push --force-with-lease` | Not if `origin/topic` is out of date. Yes if you fetched them without integrating them |
| `git push --force-with-lease=topic:<sha>` | No, unless the remote tip is exactly `<sha>` |
| `git push --force-with-lease --force-if-includes` | No, unless your local branch already includes the remote tip |
Exercise
You rebased topic with git rebase --onto origin/main next topic. Your editor ran a background git fetch, which brought a teammate's new commit into origin/topic. You did not look at it. Does a plain git push --force-with-lease origin topic protect their commit? Name one change to the command that would.
Show answer
No. The background fetch updated origin/topic, so it matches the remote and the lease passes, and the push discards the teammate's commit. Add --force-if-includes, so Git also checks that your local branch contains the remote tip. Pinning the expected tip, for example --force-with-lease=topic:<sha> with the commit you last reviewed, also works.