Sub topic 8
Pushing the rebased stack safely
This chapter assumes you can run git rebase --update-refs on a stack (covered earlier) and know how remote branches and pull requests relate. It needs Git 2.38 or newer.
Why the push is rejected
After git rebase --update-refs origin/main, feature-1, feature-2 and feature-3 all have new commit IDs. The copies on the remote still point at the old commits. The two histories have diverged, so a normal git push fails with a "non-fast-forward" error.
Each branch has to be replaced on the remote, which means a force push. A plain git push --force is risky because it overwrites the remote branch no matter what is there. If a teammate pushed a fix to feature-2 after your last fetch, --force deletes it without warning.
Use --force-with-lease
--force-with-lease is a conditional force push. Git overwrites the remote branch only if it still points where your remote-tracking branch (for example origin/feature-2) says it does. If someone else moved it, the push is refused for that branch.
git push --force-with-lease origin feature-1 feature-2 feature-3Git checks the lease separately for each branch named on the command line. One refused branch does not stop the others from updating.
Push the whole stack in one step
A half-updated stack is confusing for reviewers and for CI. Add --atomic so the remote either accepts every update or none of them:
git push --atomic --force-with-lease origin feature-1 feature-2 feature-3The remote must support atomic pushes. GitHub and GitLab do.
Check before you push
Look at the stack once more before you publish it:
git log --oneline --decorate --graph origin/main..feature-3All three branch names should appear on commits in one straight line. To compare against what is on the remote, use git range-diff:
git range-diff origin/feature-2...feature-2A rebase that only changed the base should show the commits as unchanged. Differences you did not expect point to a conflict resolution worth reviewing.
The lease can be defeated
The lease compares the remote branch with your remote-tracking branch. If you run git fetch after a teammate pushed, your tracking branch is updated and the lease passes. Your rebased branch never contained their commit, so the force push still discards it.
Git 2.30 added --force-if-includes to cover this case. It also requires that your local branch includes the commits of the remote-tracking branch you are overwriting:
git push --atomic --force-with-lease --force-if-includes origin feature-1 feature-2 feature-3Fetch deliberately, and read what arrived before you rebase and push.
What happens to the pull requests
A pull request follows its branch name, not a commit ID. After the force push, each PR shows the new commits. Keep each PR's base pointing at the branch below it: feature-2 targets feature-1, and feature-3 targets feature-2. Reviewers then see only the changes that belong to each layer. Comments attached to old commits may be marked outdated. This is normal.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Pushing only `feature-3` | Lower PRs keep old commits and drift from the top | Push every branch in the stack |
| Using `--force` | Overwrites teammates' commits silently | Use `--force-with-lease` |
| Running a plain `git push` | Rejected as non-fast-forward | Name the branches and add the lease |
| Fetching right before pushing without looking | The lease no longer protects you | Add `--force-if-includes` |
Exercise
You rebased a three-branch stack and want to publish it so that either all branches update or none do, without overwriting any commit a teammate pushed. Which command do you run, and why is --force-with-lease alone not enough for "all or none"?
Show answer
Run git push --atomic --force-with-lease --force-if-includes origin feature-1 feature-2 feature-3. --force-with-lease checks each branch separately, so some branches can update while others are refused. --atomic makes the remote accept all updates or none. --force-if-includes guards against a fetch having already moved your remote-tracking branches past commits you never saw.