Error! [rejected] main -> main (non-fast-forward)
You committed locally, pushed, and Git refused the update. Someone else pushed to the same branch first. Your history and the remote history have diverged.
Why it happens
A fast-forward is only possible when your branch is a straight extension of the remote branch. If the remote has a commit you never downloaded, Git will not overwrite that commit with your push.
A safe update
git status
git pull --rebase origin main
git push origin main
pull --rebase replays your local commits on top of the remote ones, so you do not add a merge commit for a simple update. If Git stops on a conflict, fix the file, then run git rebase --continue. If you want to abandon the rebase, git rebase --abort returns you to the state before the pull.
- Run git status and confirm you are on the branch you meant to update.
- Fetch the remote commits with git pull --rebase.
- Resolve any conflicts, continue the rebase, and push again.
- Reserve git push --force-with-lease for a branch you alone use, after you understand which commits it will replace.