What this thread does
The problem
git push ends with ! [rejected] main -> main (fetch first) or (non-fast-forward) and error: failed to push some refs to '...'.
What the sources say
- Stack Overflow, "Git error: failed to push some refs to remote" (asked by leipzy). As of November 2026, the accepted answer (VonC) says that when the remote has new commits, use
git pull --rebasethengit push; with Git 2.6+ you can setpull.rebase trueonce. It also suggestsgit push -u origin main, orpush.autoSetupRemote true(Git 2.37+), to set up tracking. - Another answer (cagcak) suggests
git push -f; Git's docs warn that it can cause the remote repository to lose commits. VonC's answer is the one to follow. - For a rejection after amending a commit that is already pushed, see the amend question (Spoike): that is the case where a force push is intended, and
--force-with-leaseis the safer form (see git push docs).
What to do
1. git fetch and git status: with an upstream configured, status reports whether your branch has diverged or is behind. 2. git pull --rebase (replays your commits on top), fix conflicts, git rebase --continue. 3. git push. 4. If you amended or rebased commits that were already pushed and nobody else uses the branch, git push --force-with-lease, never plain -f on a shared branch.
How to describe it to your agent
git push was rejected with non-fast-forward. Fetch, show me what the remote has that I do not, then pull --rebase. If there are conflicts, stop and show them. Never use push --force; if a force is needed, ask me and use --force-with-lease.
Mistakes to check
- Using
-ffirst and overwriting a teammate's commits. - Pulling on the wrong branch name.
Limits
I did not run these steps myself. The accepted answer dates from 2014 and was updated by its author; commands match current docs.
Started from a post by @leipzy, VonC (Stack Overflow); git docs, packaged by the editor. Original post