Sub topic 3
Rebasing and amending: when you need to force push
This chapter explains why rebasing or amending a commit that you already pushed makes Git reject your next push. It assumes you know how to commit, branch, push and pull, that you have used git rebase or git commit --amend, and that you know what a remote such as origin is. It also builds on the previous chapters: remote-tracking branches and the fast-forward rule.
Rewriting creates new commits
A commit's ID is a hash of its content, its parent, its author data and its message. If any of those change, the result is a different commit with a different ID. Git never edits a commit in place.
Two everyday commands do this:
git commit --amendreplaces the latest commit with a new one. It has the same parent but a different message or content.git rebasereplays your commits on top of a new base. Each replayed commit gets a new parent, so each one gets a new ID, even if the diff is identical.
The old commits are not deleted right away. They stay in the object database, and the reflog (Git's local log of where your branch tips have been) still points to them. They are just no longer reachable from your branch.
Why the push is rejected
Suppose you pushed feature/login with commits A, B and C. Then you rebased it onto a newer main. Your local branch now holds A', B' and C', which are new commits with the same changes.
The remote branch still points to C. Your local branch points to C'. C is not an ancestor of C', so the update is not a fast-forward. Git refuses it to avoid losing C, B and A on the remote.
The diagram below shows the two histories.
The rejection looks like this:
To github.com:example/repo.git
! [rejected] feature/login -> feature/login (non-fast-forward)
error: failed to push some refs to 'github.com:example/repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart.The hint suggests git pull. Do not follow it here. A pull would merge the old commits A, B and C back into your branch. You would get duplicated changes and a messy history, which undoes the point of the rebase.
The rewrite-and-push workflow
The intended fix is to tell Git that you meant to replace the remote history. This is a force push:
git switch feature/login
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature/loginThe first three commands update your view of the remote and rebase onto the newest main. The last command overwrites origin/feature/login with your rewritten branch, but only if the remote is still where you last saw it. Later chapters cover that check in detail. For now, treat --force-with-lease as the form to use, and plain --force as the form to avoid.
When force pushing is appropriate
Force pushing is a normal part of working with branches that only you use. Good cases include:
- Cleaning up a pull request branch with an interactive rebase before review.
- Rebasing a feature branch onto the latest
mainto resolve conflicts. - Fixing a typo in the last commit message with
--amend.
It is risky on a branch that others have based work on. Their copies still contain the old commits. When they next pull, Git sees two diverging histories, and they may have to untangle duplicated commits.
| Situation | Rewrite and force push? |
|---|---|
| Personal feature branch, no one else has it | Yes |
| Shared branch, with a team agreement | Only after telling everyone |
| `main` or a release branch | No |
Checking before you push
Before force pushing, compare your branch with the remote. The HEAD...origin/feature/login range with --left-right shows what each side has that the other lacks:
git log --oneline --left-right HEAD...origin/feature/loginLines starting with < are only in your branch. Lines starting with > exist only on the remote. After a clean rebase, you expect the > lines to be your old commits (B, C and so on). If you see a > commit that you do not recognise, someone else pushed to the branch. Stop and look at it before overwriting.
Exercise
You amended your latest commit message and git push was rejected as a non-fast-forward. Why is git pull the wrong response, and what should you do instead?
Show answer
The amend replaced the old commit with a new one, so the remote still has the old commit that is not an ancestor of your branch. git pull would merge the old commit back in and duplicate the change. Check that the remote has no commits you do not know about, for example with git log --oneline --left-right HEAD...origin/<branch>. Then force push with git push --force-with-lease origin <branch>.