Skip to main content
Git & Version Control · Developer Error Resolution Lab

Fix Git 'error: failed to push some refs to' (Updates were rejected because the remote contains work)

Resolve rejected pushes caused by remote commits you do not have locally, protected branches, or secret-scanning push protection.

Triggered Error Message:

! [rejected] main -> main (fetch first) error: failed to push some refs to 'github.com:user/repo.git'

Customize Solution for Your Environment
# See what the remote has that you don't
git fetch origin
git log --oneline HEAD..origin/main

# Rebase your commits on top and push
git pull --rebase origin main
git push origin main

# Only if you intentionally rewrote history (never on shared branches):
git push --force-with-lease origin main

Root Cause & Mechanism Analysis

Why does this error occur?

The remote branch has commits your local branch does not contain (someone else pushed, or you edited on GitHub), so a fast-forward push is impossible. It can also be triggered by branch protection rules or GitHub push protection detecting a secret.

Frequently Asked Questions

Should I use git push --force?
Use `--force-with-lease` instead: it refuses to overwrite remote commits you have not fetched, protecting teammates' work.
The push is rejected with GH013 even after pulling. Why?
GitHub push protection found a secret in your commits. Remove it from history and rotate the key — scan locally first with `npx castov scan .`.
DEVSECOPS & CI/CD PARTNER2 Slots Open

Need Zero-Knowledge .env Secret Sync Across Your Engineering Team?

Explore Castov Env Vault for client-side AES-256-GCM encrypted environment sharing, or sponsor this engineering hub.

Browse All 69 Guides