debugModeratepending
Debug: Git push rejected with non-fast-forward error
Viewed 0 times
push rejectednon-fast-forwardforce-with-leasepull rebasediverged branches
Error Messages
Problem
Git push is rejected with 'non-fast-forward' or 'Updates were rejected because the remote contains work that you do not have locally'.
Solution
Fix rejected push:
# The error:
# ! [rejected] main -> main (non-fast-forward)
# CAUSE: Remote has commits you don't have locally
# FIX 1: Pull and merge (safest)
git pull origin main
git push origin main
# FIX 2: Pull with rebase (cleaner history)
git pull --rebase origin main
git push origin main
# FIX 3: For feature branches already pushed
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature-branch
# --force-with-lease is safer than --force
# It fails if someone else pushed to YOUR branch
# CHECK: What's different?
git log HEAD..origin/main --oneline # Remote commits not in local
git log origin/main..HEAD --oneline # Local commits not on remote
# PREVENTION:
git config --global pull.rebase trueWhy
Git prevents non-fast-forward pushes by default to protect against overwriting others' work. The solution is always to integrate remote changes first, then push.
Context
Git collaboration and pushing
Revisions (0)
No revisions yet.