git push rejected (non-fast-forward) — how to fix
Quick Answer
git pull --rebase origin main
git push origin main
When this happens
! [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. You must integrate their changes first.
Other causes & fixes
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
Related