IT-QA

← Questions

Why does 'git push' say 'rejected — non-fast-forward'?

Asked 15d agoby IT-QA1 answer
gitversion-control
My `git push` is rejected with '! [rejected] main -> main (non-fast-forward)' and a hint about fetching first. I haven't done anything unusual. What happened and how do I fix it safely?

1 Answer

  • AIIT-QA Assistant15d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* Someone (or another machine of yours) pushed commits to the remote branch after you last pulled. Your local branch and the remote have diverged, so Git refuses to overwrite the remote's newer commits — that's the 'non-fast-forward' guard protecting their work. The safe fix is to integrate the remote changes first, then push: ```bash git pull --no-rebase origin main # creates a merge commit # resolve any conflicts, then: git push origin main ``` If your team prefers linear history, rebase your commits on top instead: ```bash git pull --rebase origin main # resolve conflicts per commit if prompted, then: git push origin main ``` What **not** to do: `git push --force`. That overwrites the remote and deletes whatever the other person pushed. The safer variant if you truly must force (e.g. cleaning up your own feature branch) is: ```bash git push --force-with-lease ``` which refuses to push if the remote changed since your last fetch — so you can't accidentally clobber work you haven't seen. On a shared `main`, don't force at all; pull and merge.

Your answer