Skip to main content

Git · 4 common causes

rejected: non-fast-forward

Updated

The remote branch has commits you do not have locally, so your push would erase them.

The exact message

$ git push origin main
To github.com:you/project.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:you/project.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.

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is rejected: non-fast-forward.

How to fix it

Cause 1 of 4

Someone else pushed since you last fetched.

Bring their work in first: git pull --rebase replays your commits on top of theirs and keeps history linear. If a conflict appears, resolve the files, git add them, then git rebase --continue. Plain git pull also works and creates a merge commit.

Cause 2 of 4

You amended or rebased commits that were already pushed.

The remote still holds the originals, so your rewritten history is a different line. On a branch only you use, git push --force-with-lease is the right tool: it aborts if the remote moved since your last fetch, which plain --force does not check. Never force-push a shared branch such as main without agreement from everyone working on it — it rewrites history under their feet.

Cause 3 of 4

You are pushing at a branch you did not mean to.

git branch -vv shows what your branch tracks. Push explicitly at the intended target: git push origin HEAD:feature/my-work.

Cause 4 of 4

You do not yet know what you are missing.

git fetch origin then git log --oneline HEAD..origin/main lists exactly the commits on the remote that you lack, and origin/main..HEAD lists yours. Decide between rebase and merge with that in front of you rather than guessing.

Stopping it coming back

Fetch before you start work and pull before you push, and treat any branch someone else may have pulled as append-only — rewrite only branches that are yours alone.

rejected: non-fast-forward — common questions

What does --force-with-lease actually check?
That the remote branch still points where your last fetch said it did. If someone pushed in the meantime, the push is refused instead of discarding their commit. It is strictly safer than --force and costs nothing.
Should I use pull --rebase or a merge?
Rebase gives a linear history and is ideal for your own unpushed work. Merge is safer for commits others may already have, because it does not rewrite anything. Many teams set pull.rebase for feature branches and merge for integration.
I force-pushed and lost a colleague's commit. Can it be recovered?
Usually yes, if they still have it locally — their reflog holds it and they can push it again. On the server, GitHub and GitLab keep unreachable objects for a while and support restoring a branch from the events log. Ask before assuming it is gone.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor