Fix "failed to push some refs" (! [rejected] fetch first) in Git
! [rejected] main -> main (fetch first) error: failed to push some refs to 'github.com:user/repo.git'
- ! [rejected] main -> main (non-fast-forward)
- hint: Updates were rejected because the remote contains work that you do not have locally.
- hint: Updates were rejected because the tip of your current branch is behind its remote counterpart.
- error: src refspec main does not match any
- ! [remote rejected] main -> main (protected branch hook declined)
The remote repository has commits you do not have locally, so Git refuses to overwrite its history. Fetch them with git pull --rebase origin main, resolve any conflicts, then run git push again.
What this error means
By default, a git push is only accepted if it is a fast-forward: the remote branch must be an ancestor of your local branch, so no remote commit is lost. If someone (or you from another machine or the GitHub web UI) pushed in the meantime, or if you rewrote your history with rebase or commit --amend, the two histories diverge and the server rejects the update. The reason in parentheses matters: fetch first and non-fast-forward mean "integrate the remote first", remote rejected comes from a server rule, and src refspec ... does not match any means the local branch does not exist.
Common causes
- New commits on the remote (
fetch first): a teammate pushed, or you edited a file (README…) directly on GitHub. - Rewritten local history (
non-fast-forward): aftergit rebase,git commit --amendorgit reset, your branch no longer contains the commits already pushed. - Branch does not exist (
src refspec main does not match any): no commit yet, or your branch is calledmasterand you pushmain. - Protected branch (
remote rejected,protected branch hook declined): the server forbids direct pushes and requires a pull request or checks. - Remote repository created with a README while your local repository was initialised separately: the histories have no common ancestor.
Step-by-step fix
-
Read the rejection reason
Look at the text in parentheses after
! [rejected]or! [remote rejected], then compare your history with the remote.git fetch origin git status git log --oneline --graph --all -n 20 -
Integrate the remote commits
Replay your local commits on top of the remote ones. Rebase keeps a linear history; if you prefer a merge commit, use
git pull --no-rebase origin main.git pull --rebase origin main -
Resolve conflicts
If Git stops on a conflict, open the listed files, keep the right content between the
<<<<<<<and>>>>>>>markers, then continue.git rebase --abortcancels everything and brings you back to where you were.git status # edit the conflicted files, then: git add path/to/file git rebase --continue -
Push again
Your branch now contains the remote history: the push is a fast-forward and goes through.
git push origin main -
If you rewrote history on purpose
Only on your own working branch, after an intentional rebase: use
--force-with-lease, which refuses to overwrite the remote if someone pushed since your last fetch. Never use--forceonmainor a shared branch: you would erase other people’s work.git push --force-with-lease origin my-feature -
"src refspec does not match any" or protected branch
Check your branch name and that at least one commit exists, then push the current branch. For a protected branch, push a feature branch and open a pull request.
git branch --show-current git log --oneline -n 1 git push -u origin HEAD
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Run
git pull --rebasebefore you start working and before each push. - Set the default behaviour with
git config --global pull.rebase trueto avoid needless merge commits. - Work on feature branches and protect
main: only pull requests get in.
Frequently asked questions
Can I just run git push --force?
Only if you are sure the remote commits must disappear, for example on your personal branch. Prefer --force-with-lease, and never on a shared branch.
What is the difference between fetch first and non-fast-forward?
fetch first: the remote has commits you never fetched. non-fast-forward: you already fetched them but your branch diverged. In both cases, integrate the remote with git pull --rebase.
Why do I get "hint: You have divergent branches" when I run git pull?
Since Git 2.27, Git asks how to reconcile two divergent histories. Pick a strategy: git pull --rebase once, or git config --global pull.rebase true for good.
Useful tools for this error
More Git & GitHub errors
- Fix "fatal: refusing to merge unrelated histories" in Git
- Fix "Permission denied (publickey)" with Git and GitHub
- Get out of "detached HEAD" state in Git without losing commits
- Fix "Your local changes to the following files would be overwritten by merge" in Git
- Resolve "CONFLICT (content): Merge conflict" and "Automatic merge failed" in Git
- Fix "Support for password authentication was removed" on GitHub
- Fix "GH001: Large files detected" (file over 100 MB) on GitHub