Sub topic 11
Publishing a rebased branch with --force-with-lease
This chapter assumes you can run git rebase --onto, read git log --graph, and use git push with a remote such as origin. It also assumes you know that a remote-tracking branch such as origin/topic is your local record of where the remote branch was at your last fetch or push. The earlier chapters showed how to rebase. This one covers what happens when you publish the result.
Why the push is rejected
A rebase does not move commits. It creates new commits with the same changes and different parents, then points your branch at them. If topic was already pushed, the remote branch still points at the old commits.
Suppose you rebased topic onto main with git rebase --onto main next topic. The remote's topic and your local topic now disagree:
remote topic: A---B---C---D (old commits, based on next)
local topic: A'---B'---C'---D' (rewritten, based on main)Neither tip is an ancestor of the other. A normal push is only allowed when it is a fast-forward, meaning the remote tip is an ancestor of what you send. This one is not, so Git refuses:
! [rejected] topic -> topic (non-fast-forward)
error: failed to push some refs to 'origin'Do not "fix" this with git pull. Pulling merges or rebases the old remote commits back into your branch. That reintroduces the commits you just replaced and produces duplicates. The correct response is to replace the remote branch deliberately.
Plain force versus a lease
git push --force tells the remote to set the branch to your commit, whatever it currently holds. If a teammate pushed a commit to topic after your last fetch, a forced push deletes it from the branch without warning.
--force-with-lease adds a condition: the remote branch must still be where you last saw it. Git compares the remote's current value with your remote-tracking branch, origin/topic. If they match, the push goes through. If they differ, someone else has pushed, and Git rejects the push.
git push --force-with-lease origin topicA rejected lease looks like this:
! [rejected] topic -> topic (stale info)
error: failed to push some refs to 'origin'"Stale info" means your picture of the remote is out of date. Fetch, look at what arrived, and decide what to do. Do not retry with --force.
The lease can be defeated by fetching
The lease compares against origin/topic as it is now, not as it was when you started the rebase. Anything that updates origin/topic also updates the value the lease expects. The usual cause is a git fetch, which some editors and tools run in the background. Consider this sequence:
- You rebase
topiclocally. - A teammate pushes commit
Etoorigin/topic. - Your IDE runs
git fetch, andorigin/topicnow includesE. - You run
git push --force-with-lease origin topic.
The lease now matches the remote exactly, so the push succeeds and E is discarded from the branch. You never looked at E. The lease only protects you from commits you have not fetched. It does not tell you whether you have reviewed what you fetched.
You can defend against this in three ways.
Inspect before you push
After a rebase, ORIG_HEAD holds your branch tip from before the rebase. Any commit on the remote branch that was not in your old tip came from someone else:
git fetch origin
git log --oneline ORIG_HEAD..origin/topicIf this prints nothing, the remote branch contains only the commits you rewrote, and the push is safe. If it prints commits, a teammate has added work. Bring those commits into your rebased branch first, or ask the teammate. ORIG_HEAD is overwritten by later commands such as git reset, git merge, or another rebase. If you have run any, use the reflog entry for the branch instead, for example topic@{1}.
State the expected value explicitly
You can give the lease the exact value you expect, so background fetches no longer matter:
git push --force-with-lease=topic:$(git rev-parse ORIG_HEAD) origin topicThe remote branch topic is overwritten only if it currently points at the commit you had before rebasing. This is the strictest form, and it works in scripts. The cost is that you must get the commit ID right.
Add --force-if-includes
Git 2.30 added --force-if-includes. It is used together with --force-with-lease (without an explicit expected value). It also requires that the remote-tracking tip is something you have seen locally, based on your branch's reflog. That catches the case where a fetch brought in a commit you never integrated.
git push --force-with-lease --force-if-includes origin topicSet git config push.useForceIfIncludes true to make it the default when you use a lease. It does not replace reading the log, but it closes the background-fetch gap in most workflows.
Comparing the options
| Command | Overwrites remote when | Protects unseen commits | Protects commits fetched but not integrated |
|---|---|---|---|
| `git push` | Only on a fast-forward | Yes | Yes |
| `git push --force` | Always | No | No |
| `git push --force-with-lease` | Remote equals `origin/topic` | Yes | No |
| `git push --force-with-lease=topic:<sha>` | Remote equals `<sha>` | Yes | Yes, if `<sha>` is your pre-rebase tip |
| `git push --force-with-lease --force-if-includes` | Remote equals `origin/topic` and you have integrated it | Yes | Yes |
A complete publishing sequence
This sequence rebases a pushed branch and publishes it. It uses the --onto command from the earlier chapters.
# 1. Preview the commits that will be replayed.
git log --oneline next..topic
# 2. Rebase onto main, dropping the dependence on next.
git rebase --onto main next topic
# 3. Check that the result looks right.
git log --oneline --graph --decorate main topic
# 4. Fetch and look for commits from other people.
git fetch origin
git log --oneline ORIG_HEAD..origin/topic
# 5. Publish with a lease.
git push --force-with-lease --force-if-includes origin topicIf step 4 prints commits you did not write, stop. Do not push until you have dealt with them.
When rewriting history is unsafe
Force-pushing is safe when you are the only person who relies on the branch. It becomes a problem when others do.
- Shared branches. If a teammate has based work on
origin/topic, your rewrite leaves them with commits whose parents no longer exist on the remote. Their next pull will try to merge old and new versions of the same changes, and it will conflict or duplicate commits. - Default and release branches.
mainand release branches should never be rewritten. Most hosting services protect them and reject force pushes. - Open reviews. Rewriting a branch under an open pull request can detach existing review comments from the commits they were attached to. Some teams ask you to add fixup commits during review and rebase only once, just before merging. Follow your team's rule.
- Branches other branches depend on. If
feature-bwas built on yourtopic, rebasingtopicstrandsfeature-bon the old commits. Restack it with--onto, using the old tip oftopicas the upstream.
Helping a teammate who based work on your old branch
Suppose a teammate has local work on top of your old topic, and you have force-pushed. They can move their work to the new branch with the same --onto pattern. The upstream is the old tip that their commits sit on. They should fetch, then find the old tip in their reflog or in the notes you give them, and run:
git fetch origin
git rebase --onto origin/topic OLD_TIP their-branchHere OLD_TIP stands for the commit ID of the pre-rebase topic tip. Without it, a plain git rebase origin/topic would replay your old, already-rewritten commits as well, causing conflicts. The range OLD_TIP..their-branch contains only their own commits.
A short message with the old tip's commit ID, sent when you force-push a shared branch, saves the recipient a search through the reflog.
Publishing several rebased branches
If you restacked a chain of branches, each one has been rewritten and each needs publishing. With --update-refs (Git 2.38 or later), the rebase moves the intermediate branches for you. You can then push them in one command. Each branch still gets its own lease check:
git push --force-with-lease origin feature-a feature-bThe remote may also reject a push if a branch's pull request base has changed. Push the lower branch first, then the one that depends on it, so the hosting service can retarget the pull requests in order.
Pushing only after you are sure
Before you publish, you can still change your mind. A force-push is the point where the old commits stop being reachable on the remote. They remain in your reflog for a while (90 days by default for reachable entries, 30 days for unreachable ones), but a teammate's clone may not have them, and the remote's own copy may be pruned. If a push goes wrong, git reset --hard ORIG_HEAD or the reflog repairs your local branch. Repairing the remote means pushing the old commit back from a clone that still has it:
git push --force-with-lease=topic:NEW_TIP origin OLD_TIP:refs/heads/topicHere NEW_TIP is the commit you pushed by mistake and OLD_TIP is the one to restore. The lease ensures you only undo your own push and not a later one.
Exercise
You rebased topic and ran git push --force-with-lease origin topic. Git replied ! [rejected] topic -> topic (stale info). What does that mean, and what should you do next?
Show answer
The remote topic no longer points at the commit your origin/topic remote-tracking branch records, so someone pushed after your last fetch. Nothing was overwritten. Run git fetch origin, then git log --oneline ORIG_HEAD..origin/topic to see the new commits. Integrate them into your rebased branch, or talk to their author, and then push again. Do not switch to --force.
Exercise
Your editor fetches in the background. Write a push command that only overwrites the remote topic if it still points at the commit your branch had before the rebase. Assume ORIG_HEAD is still intact.
Show answer
git push --force-with-lease=topic:$(git rev-parse ORIG_HEAD) origin topic. The explicit expected value ignores whatever a background fetch has stored in origin/topic, so the push is rejected if anyone else has pushed since.
Exercise
A teammate has two local commits on top of your old topic tip, abc1234. You force-pushed the rebased topic. Which command should they run to move their commits onto your new branch, and why not a plain git rebase origin/topic?
Show answer
git rebase --onto origin/topic abc1234 their-branch. The range abc1234..their-branch contains only their two commits. A plain git rebase origin/topic would also try to replay your old commits, which already exist in rewritten form on origin/topic, and it would cause conflicts or duplicates.