Sub topic 8
Pushing the rebased stack safely
This chapter assumes you have already run git rebase --update-refs on a stack, as in the previous chapters. It also assumes you know what a remote-tracking branch such as origin/feature-1 is, and that you have Git 2.38 or newer. The chapter covers what happens when you publish the result.
Why a rebased stack needs a force push
A rebase does not edit commits. It creates new commits with the same changes and moves the branch name to them. Each new commit has a different ID from the old one, because the parent changed.
After git rebase --update-refs origin/main on a three-branch stack, feature-1, feature-2 and feature-3 all point at new commits. The copies on the remote still point at the old commits. The old and new histories have diverged, so a plain git push is rejected as a non-fast-forward.
You have to overwrite the remote branches. The question is how to do that without destroying someone else's work.
Plain --force and its risk
git push --force tells the server to set the branch to your commit whatever it currently holds. If a teammate pushed a commit to feature-2 after your last fetch, --force deletes it from the remote branch. Nothing warns you.
--force-with-lease adds a condition: "set this branch to my commit, but only if it still has the value I last saw." Your last-seen value is stored in the remote-tracking branch, for example origin/feature-2.
Pushing the whole stack in one command
Push every branch in the stack in a single command:
git push --force-with-lease origin feature-1 feature-2 feature-3Name each branch. Running git push --force-with-lease with no branch names only pushes what your push.default setting selects, which is usually just the current branch. You would update feature-3 and leave feature-1 and feature-2 on the remote with old commits. Pull requests for those branches would then show a stale diff.
To see which branches in the stack are out of sync with their remote branches, list them with their upstream status:
git branch -vvA rebased branch shows something like [origin/feature-2: ahead 3, behind 3]. Equal "ahead" and "behind" counts are the usual sign of a rebase that has not been pushed yet.
Making the push all-or-nothing
Each branch is checked separately. If the lease on feature-2 fails but the others pass, Git updates feature-1 and feature-3 and rejects feature-2. The remote stack is now half old and half new, which makes the pull requests confusing.
--atomic asks the server to apply all of the ref updates or none of them:
git push --atomic --force-with-lease origin feature-1 feature-2 feature-3Atomic pushes need server support. GitHub, GitLab and current Git servers accept them. If a server does not, the push fails with an error saying atomic push is not supported, and you can fall back to the non-atomic form.
Where the lease can be fooled
The lease compares the remote's value with your remote-tracking branch. It does not know whether you have seen or merged the commits behind that value. That causes a known failure:
- A teammate pushes a fix to
origin/feature-2. - Your editor or a script runs
git fetchin the background. Yourorigin/feature-2now matches the remote. - You rebase your local
feature-2, which does not contain the teammate's fix. - You push with
--force-with-lease. The remote value equalsorigin/feature-2, so the lease passes, and the fix is overwritten.
Git has two defenses.
--force-if-includes
--force-if-includes (Git 2.30 and later) adds a second check. Git looks at the reflog of your local branch and requires that the current remote-tracking commit appears in it, which shows you integrated it locally.
git push --force-with-lease --force-if-includes origin feature-1 feature-2 feature-3In the scenario above, the teammate's commit was fetched but never part of your local feature-2, so the push is rejected. You can enable this check for every push through push.useForceIfIncludes:
git config --global push.useForceIfIncludes truePinning the expected value
You can state the value yourself, so a background fetch cannot change it:
git push --force-with-lease=feature-2:5d6e7f8 origin feature-2Here 5d6e7f8 is the commit you saw on the remote before you began the rebase. This is precise but tedious across a stack. Use it when you suspect a teammate shares one of the branches.
Reading a rejection
A failed lease prints a line like this:
! [rejected] feature-2 -> feature-2 (stale info)
error: failed to push some refs to 'origin'"Stale info" means the remote branch is not where your remote-tracking branch says it is. Do not respond by retrying with --force. Instead:
- Run
git fetch originto updateorigin/feature-2. - Compare what is on the remote with what you have:
git log --oneline feature-2..origin/feature-2. - If the output lists commits, someone else pushed. Rebase or cherry-pick those commits into your branch, or ask the author.
- If the output is empty, the remote holds nothing you lack. Push again.
Step 2 checks only that the remote has no commits missing from your local branch. After a rebase, old commits on the remote usually appear in that log because their IDs differ. Look at the commit messages and authors. Lines that duplicate your own rebased commits are expected. Lines from other people are the ones that matter.
Order and pull requests
Hosting services such as GitHub compute a pull request's diff against its base branch. In a stack, the pull request for feature-2 has feature-1 as its base. When you push the whole stack in one command, all three branches update within a second or two and the diffs stay consistent.
If you push the branches one at a time, there is a short window where feature-2 is rebased but feature-1 is not. The pull request for feature-2 then shows extra commits. This fixes itself after the last push, but reviewers who open the page in the meantime may be confused. Pushing the whole stack in one command avoids that. If you must push one by one, go bottom to top: feature-1, then feature-2, then feature-3.
Never retarget a pull request or delete a base branch in the middle of a push sequence. Wait until all branches are updated.
A safe routine
Use this sequence each time you restack and publish:
git fetch origin
git switch feature-3
git rebase --update-refs origin/main
git branch -vv
git push --atomic --force-with-lease --force-if-includes origin feature-1 feature-2 feature-3The git branch -vv step is a check. If a branch you expected to move still shows its old position, the rebase did not move it (see the limits discussed earlier), and you should fix that before pushing.
Summary of options
| Option | What it protects against | Notes |
|---|---|---|
| `--force` | Nothing | Overwrites whatever the remote holds. Avoid it on shared branches. |
| `--force-with-lease` | Overwriting a remote branch that moved since your last fetch | Fooled if you fetched without integrating the new commits. |
| `--force-with-lease=ref:commit` | The same, against a value you name | Not affected by background fetches. |
| `--force-if-includes` | Overwriting fetched commits you never integrated | Git 2.30 or newer. Works only with `--force-with-lease` that has no explicit value. |
| `--atomic` | A half-updated stack | The server must support it. |
Exercise
You rebased a three-branch stack and ran git push --force-with-lease origin from feature-3 with no branch names. The push succeeded, but the pull requests for feature-1 and feature-2 still show old commits. Why, and what command fixes it safely?
Show answer
With no branch names, Git pushes only what push.default selects, which is normally just the current branch feature-3. The remote copies of feature-1 and feature-2 were never updated. Push them explicitly: git push --atomic --force-with-lease --force-if-includes origin feature-1 feature-2 feature-3.
Exercise
A teammate pushed a commit to origin/feature-2. Your editor then fetched in the background. You rebased your stack and pushed with --force-with-lease, and the push was accepted. What happened to the teammate's commit, and which option would have prevented it?
Show answer
The push overwrote it. The background fetch moved origin/feature-2 to the teammate's commit, so the lease saw a matching value. Your local feature-2 never contained that commit. Adding --force-if-includes would have rejected the push, because the fetched commit was not in your local branch's reflog. Pinning the expected value with --force-with-lease=feature-2:<commit> would also have caught it.