Push Rejected
Error message
! [rejected] main -> main (fetch first)
Quick summary
Your push was rejected because the remote branch has newer commits — you need to pull and integrate them first.
Why this happens
Someone else pushed commits to the remote that you don't have locally
Your local branch is behind the remote branch
Force push is disabled on the remote branch (e.g., protected
main)
Minimal example
git push # ! [rejected] main -> main (fetch first)git pull --rebase
git push # now succeedsThe remote has commits you don't have — git pull --rebase integrates them first, then git push succeeds.
How to diagnose
Run
git status— does it say your branch is behind the remote?Run
git fetchto download remote changes without mergingCheck
git log origin/main..mainto see how your local commits differ
How to fix
Run
git pull(orgit pull --rebase) to integrate remote changesResolve any merge conflicts that arise from the pull
Run
git pushagain after the pull succeeds
How to prevent
Pull before you push to stay in sync with the remote
Push frequently in small increments to avoid large divergences
Related Resources
Related Lessons
- What is Git?
Learn about pushing and pulling in Git
Related Practice
- Understanding Git Commits
Understand Git commits and pushing.