You’ve committed your work, you run git push, and Git refuses with a wall of text ending in failed to push some refs. A git push rejected error looks alarming, but it’s Git protecting you. In nearly every case the remote branch has commits you don’t have yet, and Git won’t let your push throw them away. Here’s how to read the message and fix it without losing anyone’s work.
Read the rejection first
The line in square brackets tells you which problem you have. The two common ones look like this:
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:you/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally.
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:you/project.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart.
Both mean the same thing in practice: the remote branch has moved on since you last pulled. Someone else pushed, you pushed from another machine, or you edited a file in the GitHub web interface. Your history and the remote’s history have split, and Git can’t simply add your commits on top.
Fix 1: pull, then push
The standard fix is to bring the remote changes in, combine them with yours, and push again:
git pull origin main
git push origin main
By default git pull fetches the remote commits and merges them into your branch, creating a merge commit if both sides changed. If the same lines changed on both sides you’ll hit a merge conflict. How to resolve Git merge conflicts walks through that step by step. Once the merge is committed, the push goes through.
Fix 2: pull with rebase for a cleaner history
If you’d rather not have a merge commit every time you sync, rebase instead. This replays your local commits on top of the remote ones, so history stays in a straight line:
git pull --rebase origin main
git push origin main
If a conflict comes up during the rebase, fix the files, then run git add on them and git rebase --continue. If it gets messy, git rebase --abort puts everything back as it was. To make rebase the default for every pull, set it once:
git config --global pull.rebase true
Only rebase commits you haven’t pushed yet. Rewriting commits that other people already have is what causes real trouble.
Fix 3: force push, but only with a lease
Sometimes the rejection is expected. You rebased or amended your own feature branch, and now your local history is deliberately different from the remote. In that case you do want to overwrite the remote, but use the safer form:
git push --force-with-lease origin my-feature
--force-with-lease only overwrites the remote branch if it still points where you last saw it. If someone pushed in the meantime, it refuses, instead of silently deleting their commits the way a plain --force would. Never force push to a shared branch such as main unless your whole team has agreed to it. If you amended a commit and that’s why the push failed, how to undo the last Git commit explains which undo methods rewrite history and which don’t.
Other reasons a push gets rejected
Not every failed to push some refs message is about diverged history. Check the lines above it for these:
- src refspec main does not match any: the branch you named doesn’t exist locally. Either you haven’t made a first commit yet, or your branch is called
master, notmain. Rungit branchto check - protected branch hook declined: the hosting service is blocking direct pushes to that branch. Push to a new branch and open a pull request instead
- Permission denied or Authentication failed: Git can’t prove who you are. Check your SSH key or access token, not your commits
- pre-receive hook declined: a server-side rule rejected the push, for example a file that’s too large or a secret detected in a commit. The message usually says which
- Wrong remote: you’re pushing somewhere you didn’t mean to. Run
git remote -vto see whereoriginpoints
If the remote URL is the problem, how to view and change remote origins in Git shows how to fix it with git remote set-url.
How to avoid it next time
- Pull before you start work, not just before you push
- Work on a short-lived branch instead of committing straight to
main - Push small commits often, so there’s less to reconcile
- Set
pull.rebase trueif your team prefers linear history
Quick checklist
- Read the bracketed reason:
fetch firstornon-fast-forwardmeans the remote moved on - Run
git pull(orgit pull --rebase), resolve any conflicts, then push - Use
--force-with-lease, never plain--force, on your own rewritten branches - For other messages, check the branch name, permissions and remote URL
Leave a Reply
You must be logged in to post a comment.