! [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
Someone else pushed changes to the remote branch since your last pull. Git wonβt let you overwrite their work.
Fix 1: Pull and Merge (Safest)
git pull origin main
# Resolve any merge conflicts if they appear
git push
Or with rebase for a cleaner history:
git pull --rebase origin main
git push
Fix 2: Force Push (Only If You Know What Youβre Doing)
# Safe force push β checks that no one else pushed
git push --force-with-lease
# Dangerous force push β overwrites everything
git push --force
β οΈ Only force push on branches that only you use. Never force push main on a shared repo.
Fix 3: After a Rebase or Amend
If you rebased or amended a commit that was already pushed:
# This is expected after rebase/amend
git push --force-with-lease
--force-with-lease is safer than --force β it fails if someone else pushed in the meantime.
When This Happens
- You and a colleague both pushed to the same branch
- You rebased or amended a pushed commit
- You reset your local branch to an earlier commit
- CI/CD made a commit (like a version bump) that you donβt have locally
Prevention
git pull --rebasebefore starting work each day- Use feature branches instead of pushing directly to main
- Use
--force-with-leaseinstead of--forcewhen you must force push