failed to push some refs / git push rejected (non-fast-forward) — how to fix

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

Someone else pushed new commits to the remote branch since your last pull. This is the usual cause of "failed to push some refs" and the non-fast-forward error.

Alternative: pull with merge commit instead of rebase

git pull origin main        # creates a merge commit
git push origin main

Force push — last resort, never on shared branches

--force-with-lease is safer than --force: it fails if someone pushed since your last fetch.

git push --force-with-lease origin main