Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

git push "! [rejected] main -> main (non-fast-forward)" or "(fetch first)": bring the remote's commits in, then push again

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

When git push ends with ! [rejected] main -> main (non-fast-forward) followed by Updates were rejected because the tip of your current branch is behind its remote counterpart, or with (fetch first) followed by Updates were rejected because the remote contains work that you do not have locally, the remote branch holds commits your local branch does not have. Git refuses so that nobody's history is lost. Compare the two tips, bring the remote's commits in with a merge or a rebase, and push again. A forced push is only for the case where you intend to replace history.

What the error means

Git's push manual defines a fast-forward: an update from commit A to commit B is one "if and only if B is a descendant of A". It adds: "The command by default does not allow an update that is not a fast-forward to prevent such loss of history." If a colleague pushed commit A and you built commit B from the same earlier commit, your push would drop A for everyone who then builds on B.

In Git 2.47.1 the status text comes from transport.c: non-fast-forward and fetch first are two of the reasons printed after [rejected]. The advice lines come from builtin/push.c. For non-fast-forward the text is "Updates were rejected because the tip of your current branch is behind its remote counterpart. If you want to integrate the remote changes, use 'git pull' before pushing again." For fetch first the text is "Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref."

A different status, [remote rejected], is printed in the same source for a refusal reported by the server. Our own reading: that is a server rule, such as a protected branch or a hook, and merging will not clear it.

Likely causes

Cause What you see How to confirm
Someone, or a pipeline, pushed to the same branch since your last fetch non-fast-forward or fetch first The remote's tip differs from your origin/main
You amended or rebased commits you had already pushed Rejected, though nobody else pushed The manual describes this case: you push A, replace it with git commit --amend to make B, and push again
Your local branch was never updated from the remote Rejected on the first push of the day The remote's tip is not in your history
You are pushing to the wrong branch or remote Rejected on a branch you did not mean git push --dry-run names the branches
A server rule refuses the update [remote rejected] The status is not [rejected]; read the server's text

Prove the cause before changing anything

All of these only read.

1. Preview the push. The push manual says --dry-run will "Do everything except actually send the updates."

git push --dry-run origin main

The output names the local and remote branch and the reason after [rejected].

2. Ask the remote for its current tip. git ls-remote lists references in a remote repository with their commit IDs.

git ls-remote origin main

3. Show the tip your clone last recorded.

git log --oneline -1 origin/main

If the commit that step 2 printed does not begin with the same characters as the one in step 3, the remote moved after your last fetch. Our own reading: this means somebody else's work is on the remote.

4. Ask whether you rewrote pushed commits. If you ran git commit --amend or a rebase after pushing, the rejection is your own old commit, as in the manual's amend case.

Fix

Choose by cause. Where someone else's work is on the remote, bring it in; do not force.

First save the current state, so that your own commits stay reachable by name:

git branch backup-before-integrate

The git branch manual says this form creates a new branch head pointing at the current HEAD. It adds one new local branch name and changes nothing else.

The merge page says: "It is therefore recommended to always commit or stash your changes before running git merge." Check first whether anything is uncommitted; this command only reads:

git status --short

An empty result means nothing is uncommitted. If lines appear, save that work in a commit or a stash before choosing an integration below, because the back-outs below can discard it.

Choose one of the two integrations below. Each has its own warning and its own undo.

Merge the remote's work into yours

Risk: the next command changes your local branch. It fetches the remote's commits and merges them in, which can stop with conflicts that you must resolve by hand, and it adds a merge commit.

git pull --no-rebase origin main

The pull manual says --no-rebase is shorthand for --rebase=false, meaning "merge the upstream branch into the current branch". The push manual adds that a git pull "will create a merge commit".

Only if you need to back out, and the merge stopped on conflicts, the merge in progress is abandoned.

Risk: the next command abandons the merge in progress. Our own reading: the conflict resolutions made so far are dropped. The merge page says that if uncommitted changes were present when the merge started, git merge --abort "will in some cases be unable to reconstruct these changes".

git merge --abort

Only if you need to back out, and the merge finished, the branch goes back to the saved one.

Risk: the next command moves your branch back to the saved branch, so the merge commit and anything you committed after the saved branch was made drop off the branch, and it overwrites files in your working tree. The reset page says --hard will "Overwrite all files and directories with the version from" the commit you name. Uncommitted work is lost.

git reset --hard backup-before-integrate

Rebase your commits on top of the remote's

The push manual offers this as the alternative: "you can rebase your change between X and B on top of A", using git pull --rebase. The pull manual warns that rebasing "rewrites history, which does not bode well when you published that history already". Our own advice: use it only for commits that exist on your machine alone.

Risk: the next command rewrites your local commits, which get new IDs, and can stop with conflicts.

git pull --rebase origin main

Only if you need to back out, and the rebase stopped on conflicts, the rebase page says "you can undo the git rebase with git rebase --abort".

Risk: the next command abandons the rebase in progress. The rebase page says it will "reset HEAD to the original branch". Our own reading: the conflict resolutions made so far, and work you committed during the rebase, are dropped.

git rebase --abort

Only if you need to back out, and the rebase finished, the branch goes back to the saved one.

Risk: the next command moves your branch back to the saved branch, so the rebased commits drop off the branch, and it overwrites files in your working tree, so uncommitted work is lost.

git reset --hard backup-before-integrate

Push the result

First save the current state: record the remote's tip, in a file outside the repository so that a later git add . cannot commit it. The first column of the saved line is the commit ID.

git ls-remote origin main > ../remote-tip-before.txt

Risk: the next command publishes your commits to the shared branch. A push cannot be recalled from anyone who fetches afterwards.

git push origin main

Only if you need to back out: a pushed commit cannot be taken back from people who have fetched it. The saved file ../remote-tip-before.txt records the remote tip from before your push.

Replace history on purpose (only after amend or rebase of your own pushed commit)

The push manual says that only "if you are certain that nobody in the meantime fetched your earlier commit A" can you run git push --force, and calls it "a method reserved for a case where you do mean to lose history". The --force-with-lease option "fails otherwise" unless the remote is still at the value you expect. Use the commit ID that the next saved file records, from its first column, as <expect>.

First save the current state. Record the remote's tip under its own name, again outside the repository:

git ls-remote origin main > ../remote-tip-before-force.txt

Then keep your own commits reachable by name:

git branch backup-before-force

Risk: the next command replaces the remote branch's history. Pushing over a different tip with --force "can cause the remote repository to lose commits"; --force-with-lease refuses if the remote moved from <expect>, but when it goes through, it cannot be recalled from anyone who fetches afterwards. Anyone who built on the replaced commit must repair their own copy.

git push --force-with-lease=main:<expect> origin main

Only if you need to back out: a forced push cannot be taken back from people who have fetched it. Our own reading: the old tip can be pushed back only while some clone or the server still has it; its commit ID is in ../remote-tip-before-force.txt.

Example (illustrative): On build-agent1, a developer committed c0ffee1 on top of a1b2c3d and ran git push. Git printed ! [rejected] main -> main (fetch first). git ls-remote origin main printed an ID beginning e4f5a6b, while git log --oneline -1 origin/main showed a1b2c3d. A colleague had pushed e4f5a6b at 09:40. The developer saved backup-before-integrate, ran git pull --rebase origin main, resolved one conflict, and pushed. No force was needed.

Confirm the fix

  1. git push ends with main -> main and no [rejected] line.
  2. git ls-remote origin main now prints the ID of your local HEAD.
  3. After a rebase, git log --oneline shows the remote's commits first, then yours.

What to watch afterwards

  • Run git pull or git fetch before starting work and before pushing.
  • Avoid amending or rebasing commits once they are pushed to a branch others use.
  • If you rely on --force-with-lease with no value, the manual warns that it "interacts very badly" with background git fetch jobs; give it the expected value.
  • The pull manual says a pull on diverged branches needs --rebase or --no-rebase, or the pull.rebase setting.

Risks and limits

  • These commands have not been run or lab-tested.
  • A forced push can lose other people's commits, and it cannot be recalled once others have fetched.
  • git reset --hard discards uncommitted work.
  • The messages quoted are a reading of Git 2.47.1; other releases may word them differently, and advice.* settings can hide the hint lines.
  • A protected branch or server hook produces a different refusal and is a different fault from the one described here.

Sources

  • git-push — Git documentation, manual for Git 2.45.0, no date shown, accessed 3 December 2026
  • git-pull — Git documentation, manual for Git 2.45.0, no date shown, accessed 3 December 2026
  • git-rebase — Git documentation, manual for Git 2.45.0, no date shown, accessed 3 December 2026
  • git-branch — Git documentation, manual for Git 2.44.0, no date shown, accessed 3 December 2026
  • git-ls-remote — Git documentation, latest release shown, no date shown, accessed 3 December 2026
  • git-merge — Git documentation, latest release shown, no date shown, accessed 3 December 2026
  • git-reset — Git documentation, latest release shown, no date shown, accessed 3 December 2026
  • transport.c — Git source repository, tag v2.47.1, no date shown, accessed 3 December 2026
  • push.c — Git source repository, tag v2.47.1, no date shown, accessed 3 December 2026