Sub topic 2
Why Git rejects non-fast-forward pushes
This chapter assumes you can commit, branch, push and pull, and that you know what a remote such as origin is. It explains the rule behind the rejection you see after a rebase or amend. The next chapters build on it.
What a push actually does
A push asks the remote to move one of its branch pointers to a commit you choose. A branch is only a pointer to a commit. Each commit points to its parent, so a branch is the tip of a chain of commits.
Before it moves the pointer, the remote checks one thing: is the commit currently at the tip an ancestor of the commit you are sending? If it is, the move is a fast-forward. The new history contains all of the old history plus new commits on top.
If the current tip is not an ancestor of your commit, the move is a non-fast-forward. The remote would have to drop at least one commit from the branch's history, so it refuses by default.
A fast-forward and a non-fast-forward
Suppose the remote branch feature/login points to commit C. Your local branch has added D and E on top of it.
Here C is an ancestor of E. The push adds D and E and nothing is lost.
Now suppose you rebase your branch onto a newer main. Git replays your commits as new commits with new parents, so they get new hashes.
The rebased commit C' has the same changes as C, but it is a different commit. Walking back from C' never reaches C. The remote would have to discard C, so it rejects the push.
What the rejection looks like
A plain push after a rebase fails 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 non-fast-forward label is the rule described above. The hint text is generic. It says "behind" even when your branch has diverged from the remote, which is the usual case after a rebase.
Behind versus diverged
The same error appears in two different situations, and they need different responses.
| Situation | Relationship | Usual fix |
|---|---|---|
| Behind | The remote has commits you lack. Your tip is an ancestor of the remote tip. | Pull or rebase to get those commits, then push. |
| Diverged | Each side has commits the other lacks. Neither tip is an ancestor of the other. | Integrate the other side's commits. Force only if you are sure you want to drop them. |
A rebase or amend always produces a diverged state, because it replaces commits that the remote already has. You can check which case you are in:
git fetch origin
git status -sb
git log --oneline --left-right feature/login...origin/feature/loginIn the git log output, lines starting with < exist only locally and lines starting with > exist only on the remote. If both kinds appear, the branches have diverged.
The reason for the rule
The rule protects other people's work. If a teammate has pushed F on top of C, and you push a branch that does not contain F, the branch pointer moves and F is no longer reachable from it. Git does not stop you from forcing this. It only makes you ask for it explicitly.
When the only commits missing from your branch are the old copies you just rebased or amended, overwriting them is the intended outcome. The remaining question is how to do that without also overwriting commits you have never seen. That is what the later chapters on --force-with-lease address.
Exercise
You amend your last commit locally and run git push. The remote branch pointed at the commit you amended. Why is the push rejected as non-fast-forward, and is your branch behind or diverged?
Show answer
Amending replaces the old commit with a new commit that has a different hash. The remote tip, the old commit, is no longer an ancestor of your new tip, because the new commit shares the old commit's parent rather than descending from it. Each side has a commit the other lacks, so the branches have diverged. A pull would not fix this cleanly. Overwriting the remote branch requires a force push.