Errors Question & Answer

Why Git Rejects a Non-Fast-Forward Push

A non-fast-forward rejection means the remote branch contains commits your local branch does not have, so the update would discard them; fetch and integrate the remote work, then push. Force-push only on branches you control, and prefer --force-with-lease over --force.

The short answer

Git updates a remote branch only when the new tip contains the old tip as an ancestor, which is a fast-forward. If the remote branch has commits your local branch does not have, the push would remove them from the branch, so git push rejects it as non-fast-forward. The fix is to fetch the remote work and integrate it with git merge or git rebase, then push again. Force-push is appropriate only when you are deliberately rewriting history on a branch that nobody else depends on.

A minimal example

$ git push origin main
To https://example.com/repo.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://example.com/repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.

The usual safe sequence:

git fetch origin
git log --oneline --graph --decorate --all -8
git rebase origin/main    # or: git merge origin/main
git push origin main

If your own amend or rebase of already-pushed commits caused the divergence, integration rewrites them again. Confirm nobody has based work on the old tip, then replace the remote branch deliberately:

git push --force-with-lease origin feature/login

Why this happens

A ref update is a fast-forward when the current remote commit is reachable from the new commit. Git performs this check on the receiving side and rejects the update when it fails. Merging the remote branch into yours produces a new commit whose history includes both sides, so the update becomes a fast-forward. Rebasing replays your commits on top of the remote tip, which also produces a descendant.

Any operation that rewrites published commits breaks the relationship: commit --amend, interactive rebase, reset, and history filtering all replace commits with new ones that do not descend from the old tip. The server sees a non-fast-forward even though your working tree is fine.

When force-push is safe

Force-push is a statement that the remote history should be discarded. It is safe when the branch is private to you, or when the team has agreed to rewrite a feature branch and everyone has stopped pushing to it. It is unsafe on shared branches such as main or release branches, because anyone who pulled the old tip must recover their work.

Situation Default push Recommended action
Local branch is simply behind rejected git pull --ff-only, then push
You amended a commit you already pushed rejected --force-with-lease after confirming no one pulled it
You and a colleague both committed rejected merge or rebase, then an ordinary push
Shared main history needs a fix rejected revert with a new commit; do not force

--force-with-lease refuses the push if the remote ref is not where your remote-tracking ref says it is, which protects against overwriting a colleague's push made after your last fetch. Do not run git fetch immediately before a lease-protected force-push: fetching updates the tracking ref and weakens that check. Inspect the tracking ref first, or pass the expected commit explicitly with --force-with-lease=<ref>:<sha>. Prefer the lease over plain --force, which overwrites unconditionally. Server-side configuration can forbid history rewrites entirely through receive.denyNonFastForwards, in which case even a lease-protected force push is rejected.

Practical checks

  • git status reports that your branch and origin/main have diverged.
  • git log --oneline HEAD..origin/main lists commits you are missing; git log --oneline origin/main..HEAD lists yours.
  • git push --dry-run shows whether the update would be accepted.
  • git reflog shows what moved and when.
  • Check what is actually staged before committing; see staged versus unstaged changes.

Edge cases

Annotated tags are not branches: moving a pushed tag is rejected with already exists unless the push is forced. Deleting a branch is allowed by default but rejected when the server sets receive.denyDeletes. A force push to a branch protected by hosting-platform rules is rejected regardless of lease flags. When a force-push has already overwritten someone's commits, recovery depends on the server's reflog (if enabled) and each developer's local git reflog, and must happen before garbage collection runs.

Sources

Revision history

Published
Updated
— Initial publication.