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.
! [rejected] main -> main (fetch first) error: failed to push some refs to 'github.com:user/repo.git'
# 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?
The push is rejected with GH013 even after pulling. Why?
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.
Related Developer Error Solutions
Browse All 69 GuidesGitHub GH013: Push cannot contain secrets (Push Protection)
Scan and scrub accidentally committed API keys or tokens from your Git commit history using Castov Secret Scanner and `git reset`.
Git: Refusing to merge unrelated histories
Merge repositories initialized independently with different initial commits using --allow-unrelated-histories.
Git: fatal: not a git repository (or any of the parent directories): .git
Run Git from the right folder, initialize a repository, or repair a missing/corrupted .git directory and safe.directory ownership checks.