Home·Error messages

git · Git

The message

! [rejected] main -> main (non-fast-forward)

What it means

Your branch is not a descendant of the remote branch, so pushing as is would drop commits that exist on the server. It happens when you rewrote already-pushed commits with rebase, amend or reset. If the rewrite was deliberate and the branch is yours alone, use git push --force-with-lease: it refuses when the remote moved since your last fetch, so unlike plain --force it cannot silently erase a colleague's push that landed in between. If the rewrite was not deliberate, the answer is pull --rebase, not force.

The fix

git push --force-with-lease
Printed by
git
Git
29

Almost every git error is a refusal that means "doing that from this state would lose something"; fatal: means git stopped without changing anything, and the hint: lines under the first line usually carry the actual remedy.

Reading an error message

  • Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
  • If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
  • Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
  • The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
  • Before pasting a fix, check what it throws away. Some of these cannot be undone.

Common questions

Q. What does “! [rejected] main -> main (non-fast-forward)” mean?

Your branch is not a descendant of the remote branch, so pushing as is would drop commits that exist on the server. It happens when you rewrote already-pushed commits with rebase, amend or reset. If the rewrite was deliberate and the branch is yours alone, use git push --force-with-lease: it refuses when the remote moved since your last fetch, so unlike plain --force it cannot silently erase a colleague's push that landed in between. If the rewrite was not deliberate, the answer is pull --rebase, not force.

Q. How do I fix it?

git push --force-with-lease — before running it, check the explanation above for what this command discards.

Q. Which tool prints this?

git. It sits under Git, and the message runs to 6 words.

Errors nearby