Fix git push rejected (fetch first / non-fast-forward)
git push ! [rejected] (fetch first) or (non-fast-forward) means the remote has commits you lack. Use pull --rebase; force-push only with --force-with-lease.
! [rejected] main -> main (fetch first)error: failed to push some refs to '/srv/remote.git'The remote branch has commits your local branch doesn't, and pushing as-is would discard them, so Git refuses. Run git pull --rebase to replay your commits on top of the remote ones, then push. You need a force push only when you rewrote commits you had already pushed (amend, rebase) — and then use --force-with-lease, not --force.
[1] Read the word in parentheses. Both common ones describe the same problem at different moments.
| In parentheses | Meaning |
|---|---|
(fetch first) | Your local repo hasn't even downloaded the new remote commits yet |
(non-fast-forward) | You fetched them (git fetch) but haven't integrated them into your branch |
(stale info) | --force-with-lease stopped you — see [5] |
(remote ref updated since checkout) | --force-if-includes stopped you — see [6] |
Fetching alone changes only the wording; the push is still rejected.
$ git fetch originFrom /srv/remote 45604ee..48d7eb6 main -> origin/main$ git push origin mainTo /srv/remote.git ! [rejected] main -> main (non-fast-forward)error: failed to push some refs to '/srv/remote.git'hint: Updates were rejected because the tip of your current branch is behindhint: its remote counterpart. If you want to integrate the remote changes,hint: use 'git pull' before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.[2] Check what arrived on the remote. Knowing whose commits you are about to integrate makes a conflict less of a surprise.
$ git status -sb## main...origin/main [ahead 1, behind 1]$ git log --oneline main..origin/main48d7eb6 B: add b.txtahead 1, behind 1 means one commit exists only locally and one only on the remote. If you are only behind, you have nothing to push — a plain git pull catches you up.
[3] Rebase your commits onto the remote with git pull --rebase, then push. With no option and no config, git 2.43 stops and asks you to choose between merge and rebase.
$ git pullhint: You have divergent branches and need to specify how to reconcile them.…fatal: Need to specify how to reconcile divergent branches.$ git pull --rebaseSuccessfully rebased and updated refs/heads/main.$ git log --oneline --graph* a6e72fe A: add a.txt* 48d7eb6 B: add b.txt* 45604ee first$ git push origin mainTo /srv/remote.git 48d7eb6..a6e72fe main -> mainTo stop being asked, set git config --global pull.rebase true. Only your unpushed local commits move, so this rebase does not rewrite anyone else's history.
[4] Merge instead if you want the integration recorded. Use this when your team prefers merge commits.
$ git pull --no-rebase --no-editFrom /srv/remote a6e72fe..7fad772 main -> origin/mainMerge made by the 'ort' strategy. b.txt | 1 + 1 file changed, 1 insertion(+)$ git log --oneline --graph -5* a8b95e2 Merge branch 'main' of /srv/remote|\ | * 7fad772 B: edit b.txt* | a9dfe59 A: edit a.txt|/ * a6e72fe A: add a.txt* 48d7eb6 B: add b.txtIf both sides edited the same lines, rebase and merge both stop at a conflict. If you are not ready to resolve it, back out.
$ git pull --rebase…CONFLICT (content): Merge conflict in app.txterror: could not apply e3b236e... A: app.txt…$ git rebase --abort; git status -sb## main...origin/main [ahead 1, behind 1]After --abort you are back where you were before the pull. To resolve instead, edit the file, git add app.txt, then git rebase --continue (or git commit for a merge).
[5] Force-push only after rewriting commits you already pushed. After git commit --amend or an interactive rebase, the remote's old commit is no longer in your history. Don't pull (you would get the same change twice); push with --force-with-lease.
$ git commit --amend -m "A: typo fixed"[main f42458d] A: typo fixed…$ git push origin mainTo /srv/remote.git ! [rejected] main -> main (non-fast-forward)…$ git push --force-with-lease origin mainTo /srv/remote.git + c8ce267...f42458d main -> main (forced update)--force-with-lease overwrites only if the remote branch is still where you last saw it. If someone pushed in between, it refuses — where --force would have silently deleted their commit.
$ git commit --amend -m "A: typo fixed again"[main 7ae8b4e] A: typo fixed again…$ git push --force-with-lease origin mainTo /srv/remote.git ! [rejected] main -> main (stale info)error: failed to push some refs to '/srv/remote.git'To keep a way back, save the commit the remote had in a ref of its own before you overwrite it, and pass that as the expected value. If the overwrite was a mistake, push that ref back. This ran in a separate test repository, so the hashes differ from the ones above:
$ git fetch origin$ git update-ref refs/backup/main-before-force origin/main$ git log --oneline -1 refs/backup/main-before-force6d5d612 A: add line$ git push --force-with-lease=main:refs/backup/main-before-force origin mainTo /srv/remote.git + 6d5d612...75f5a8d main -> main (forced update)$ git push --force-with-lease=main:HEAD origin refs/backup/main-before-force:mainTo /srv/remote.git + 75f5a8d...6d5d612 refs/backup/main-before-force -> main (forced update)$ git log --oneline -2 origin/main6d5d612 A: add lineffe1da0 first--force-with-lease=main:<expected> overwrites only if the remote main is exactly that commit, so a background fetch can't weaken it. The last two commands are the restore: put the saved commit back, but only while the remote is still the HEAD you pushed. If someone has pushed on top of your commit since, the restore is refused too — talk to them first. Delete the ref afterwards with git update-ref -d refs/backup/main-before-force.
[6] Add --force-if-includes if your editor or IDE runs git fetch in the background. A fetch updates the remote-tracking branch, and --force-with-lease then treats that new value as "seen" and overwrites someone else's commit. The --dry-run below shows exactly that.
$ git fetch originFrom /srv/remote f42458d..860426d main -> origin/main$ git push --force-with-lease --dry-run origin mainTo /srv/remote.git + 860426d...7ae8b4e main -> main (forced update)$ git push --force-with-lease --force-if-includes origin mainTo /srv/remote.git ! [rejected] main -> main (remote ref updated since checkout)error: failed to push some refs to '/srv/remote.git'…$ git log --oneline -3 origin/main860426d B: b3f42458d A: typo fixed960b02c B: app.txt--force-if-includes also checks whether the remote tip ever made it into your branch's reflog. B: b3 was fetched but never integrated, so the push was refused and the commit survived. The option arrived in git 2.30; turn it on permanently with git config --global push.useForceIfIncludes true. The git docs themselves warn that --force-with-lease without an expected value "interacts very badly" with background fetches.
If you hit this while pushing a freshly git init-ed project and the pull then says refusing to merge unrelated histories, go to that guide. If the push fails with Permission denied (publickey) instead, it's authentication, not history — see Fix GitHub Permission denied (publickey).
Official references: git-push — --force-with-lease, --force-if-includes, Note about fast-forwards · git-pull · git 2.30 release notes · GitHub — Dealing with non-fast-forward errors