The June version of this post told you fetch first and non-fast-forward were two different problems with two different fixes. That was wrong.
I found out when I tested it on Git 2.55. One bare repository in /tmp/git-test stood in for GitHub, with two clones of it called laptop and desktop. I committed on the desktop and pushed, then committed on the laptop and pushed from there. The push bounced with fetch first. I ran git fetch and pushed again, with nothing else changed, and Git said non-fast-forward.

Same commits, same remote, two labels. The only difference is the git fetch in between.
The label only tells you whether your clone has downloaded the remote’s newer commits yet. It doesn’t tell you whose commits they are, and that’s what decides the fix. This update corrects that part, fixes a command that fails on current Git, and adds the causes the first version skipped.
TLDR
The cause of “failed to push some refs to” is on the line above the error, not in it. For
fetch firstornon-fast-forward, rungit log HEAD..origin/mainfirst. If it lists someone else’s commits, rungit pull --rebase origin mainand push again. If it lists an older copy of your own commit, you rewrote history, so usegit push --force-with-lease --force-if-includes origin main, and only on a branch nobody else pushes to.
What the line above the error tells you
Git ends every refused push with the same line: error: failed to push some refs to, then the remote. The reason sits one or two lines higher. These are the ones my test produced:
(fetch first): the remote has commits your clone has never downloaded.(non-fast-forward): your clone has seen the remote’s newer commits, and your branch doesn’t include them.[remote rejected] ... (pre-receive hook declined), or GitHub’sGH006lines: the server checked the push against its rules and said no.src refspec main does not match any: there’s nothing calledmainon your side to send.(remote ref updated since checkout): the--force-if-includessafety check stopped a force push.

Every case in this post, with its fix.
The first two are where most people land, and where my old post sent them the wrong way. fetch first turns into non-fast-forward the moment anything fetches, and that includes an editor fetching in the background while you work.
Whose commits is the remote holding?
I ran two rounds to find out what tells the cases apart. In one, a second clone committed as “Teammate” and pushed while I committed locally. In the other, I amended a commit I had already pushed.
After a fetch, git status printed the same thing both times, word for word:
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" if you want to integrate the remote branch with yours)
The push error matched too. What finally told them apart was asking Git which commits the remote has that my branch doesn’t:
git fetch origin
git log --oneline --format='%h %an %s' HEAD..origin/main

The author column answers it. Someone else’s name means pull. Your own name on an older message means you rewrote history.
If the list shows someone else’s commits, use Fix 1. If it shows an older copy of your own commit, use Fix 2. If it shows both, pull first, then deal with your own rewrite.
Fix 1: the remote has someone else’s commits
You committed locally, someone pushed to the same branch in the meantime, and now your push is refused. Pull their work in with a rebase, then push:
git pull --rebase origin main
git push origin main
Git downloads the remote commits and replays yours on top of them. Your changes end up at the tip, theirs stay where they were, and the history stays a straight line. When the two sides didn’t touch the same lines, it goes through on its own. My test printed:
From /tmp/git-test/remote
* branch main -> FETCH_HEAD
Rebasing (1/1)Successfully rebased and updated refs/heads/main.
If both sides changed the same lines, Git stops on a conflict. Fix the files it names, then:
git add .
git rebase --continue
git push origin main
If you’d rather keep a merge commit, git pull --no-rebase origin main does that instead. The history gets busier, and nothing is rewritten.
One thing changed in Git since a lot of answers online were written. Git 2.27 started warning when you ran a plain git pull without picking a strategy (release notes). On Git 2.55, the version I tested, a plain git pull on a diverged branch doesn’t warn. It stops:
hint: You have divergent branches and need to specify how to reconcile them.
...
fatal: Need to specify how to reconcile divergent branches.
Nothing gets pulled. Pass --rebase or --no-rebase each time, or set a default once with git config --global pull.rebase true (rebase) or false (merge).
Fix 2: you rewrote history you already pushed
When you amend or rebase a commit after pushing it, the commit gets a new hash. The remote still has the old one, so your branch and the remote have split, even if the code is almost the same.
Pulling makes it worse. In my test, git pull --rebase after the amend tried to replay the new version of the commit on top of the old one and stopped:
CONFLICT (content): Merge conflict in style.css
error: could not apply 4f984d3... typo fixed
If you’re stuck there now, git rebase --abort takes you back.
The fix is a force push with two checks on it:
git push --force-with-lease --force-if-includes origin main
That went through in my test, + f3e6e18...4f984d3 main -> main (forced update). A plain --force overwrites the remote without looking. --force-with-lease first checks that the remote is still where you last saw it. --force-if-includes closes a gap in that check.
When the lease doesn’t protect you
The git push documentation says a bare --force-with-lease “interacts very badly with anything that implicitly runs git fetch on the remote to be pushed to in the background”. I set that case up. The teammate pushed a footer change. My laptop fetched without pulling anything in, which is what an editor with auto-fetch does. Then I made one new commit and force pushed.

With both flags, Git refused. With --force-with-lease alone, it pushed, and the teammate’s commit was gone from the remote.
With both flags, Git refused: (remote ref updated since checkout). With only --force-with-lease, it pushed. The remote’s log afterward had my three commits and no footer. The lease compares the remote with your last fetch, and the fetch had already moved that marker forward, so the check passed.
--force-if-includes arrived in Git 2.30 (release notes). It refuses the push unless the remote’s latest commit is already part of your branch’s history. It does nothing without --force-with-lease, which is why the command above carries both.
In VS Code, background fetching is the git.autofetch setting, and it’s off by default (the Git extension’s settings). If you turned it on, or another tool fetches for you, this is the case that bites. --force-with-lease also comes up in my email privacy restrictions fix, when you rewrite author lines across several commits.
Fix 3: the branch is protected
When the server refuses a push, Git prints remote rejected instead of rejected, and the lines that start with remote: carry the server’s reason. I added a small pre-receive hook to my test remote that refuses every push to main:
remote: main is protected. Open a pull request instead.
To /tmp/git-test/remote.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to '/tmp/git-test/remote.git'
On GitHub, a protected branch with a failing required check answers with lines like these, according to GitHub’s docs on required status checks:
remote: error: GH006: Protected branch update failed for refs/heads/main.
remote: error: Required status check "ci-build" is failing
No pull or rebase gets this through, because the server is refusing the branch, not your history. Push to a new branch and open a pull request:
git checkout -b fix/my-change
git push -u origin fix/my-change
That printed * [new branch] in my test. I haven’t run this against GitLab or Bitbucket, so I can’t show you their exact wording. The rule I’d follow there is the same: read the remote: lines first.
Fix 4: “src refspec main does not match any”
This one has no [rejected] line at all. A refspec is Git’s name for the “from where, to where” part of a push, and src is your side. The error means there’s nothing on your side by that name.

A brand-new repository, before the first commit.
I got it two ways. The first was pushing from a new repository before making a commit. The second was pushing main from an older project whose branch is still called master. These two commands tell you which case you’re in:
git log --oneline -1
git branch --show-current
If the first says the branch doesn’t have any commits yet, make a commit, then push. If the second prints master and you’re pushing main, rename the branch and push again:
git branch -M main
git push -u origin main
Fix 5: a new GitHub repo with a README
This one shows up on the first push to a new repo. You git init a project, create a GitHub repo with the “Add a README file” box ticked, add the remote, and push. The two sides have separate histories with no commit in common.
The push itself only said fetch first. The words “unrelated histories” showed up when I tried to merge: fatal: refusing to merge unrelated histories.
The June post also told you to run git pull origin main --allow-unrelated-histories. That’s wrong on current Git. On Git 2.55, with no pull strategy set, the command stops at fatal: Need to specify how to reconcile divergent branches. before it gets anywhere near the merge.
Two versions worked. The rebase one needed no extra flag:
git pull --rebase origin main
git push -u origin main
If you’d rather merge, this printed Merge made by the 'ort' strategy. and the push went through:
git pull --no-rebase --allow-unrelated-histories origin main
git push -u origin main
If your project has its own README.md, expect a conflict on that file either way. Keep the text you want, git add README.md, and continue. Easier still: create the GitHub repo empty and add the README in your next commit.
Why VS Code says “Can’t push refs to remote”
Push from the Source Control panel and you don’t see Git’s output. You get a popup:
Can't push refs to remote. Try running "Pull" first to integrate your changes.
The Git extension’s source decides when it shows. Any push whose output contains error: failed to push some refs to gets that popup. The only exceptions are the two force-with-lease refusals, which have their own messages (git.ts and commands.ts).
So VS Code tells you to pull for every cause in this post. That’s right for Fix 1 and wrong for the rest. After an amend, it lands you in a conflict. Click Open Git Log in the popup, find the line above the error, and pick your fix from there.
Less common causes
Large files: GitHub blocks files over 100 MiB and warns from 50 MiB (GitHub’s docs on large files). Take the file out of the commit with git rm --cached and look at Git LFS if you need to track it.
A secret in the commit: GitHub’s push protection blocks API keys and tokens. I wrote that one up in the push declined due to repository rule violations guide, including how to scrub the secret from history.
Errors that look close but aren’t this one
This post used to list a wrong remote and an expired login as causes. I ran both on Git 2.55, and neither one printed failed to push some refs to. Git stops before it gets that far, so if you see these, you have a different problem.
A remote pointing to a GitHub repo that doesn’t exist, pushed over SSH with my key:
$ git push origin main
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
A remote pointing to a local folder that isn’t a repository ended the same way, with fatal: '/tmp/git-auth-test/remote.git' does not appear to be a git repository on top instead of Repository not found. For both, git remote -v shows where origin points, and git remote set-url origin plus the right URL fixes it.
A made-up token over HTTPS, with my saved login switched off so Git had to use it:
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for 'https://github.com/wcwiz-push-test/no-such-repo.git/'
And an SSH key GitHub won’t take printed Permission denied (publickey) after the host name, then the same Could not read from remote repository lines. For a bad token, make a new one in GitHub’s token settings. For SSH, GitHub’s SSH test tells you whether your key works, and if it doesn’t, add it to your account again.
The habit that prevents the most common case
Run git pull --rebase origin main before you start working, not after you’ve committed and been refused. It does nothing for a protected branch or a missing branch name. It does keep your branch from falling behind on a shared repo, across two machines, or when an AI coding tool commits for you from another session.
And if you ever do need a force push, type both flags. In my test, the lease alone deleted a commit, and the output looked like any other successful push.
Questions
What does “error: failed to push some refs to” mean?
Git refused to update a branch on the remote. The error line is the same for every cause. The reason is on the line above it. Usually that’s the line starting with !: fetch first, non-fast-forward, remote rejected, or remote ref updated since checkout. src refspec ... does not match any is the one exception: it has no ! line and prints as its own error: line instead.
Is “fetch first” different from “non-fast-forward”?
Not in what’s wrong. In my test, the same push said fetch first before a git fetch and non-fast-forward after it. Both mean the remote has commits your branch doesn’t include. Run git log HEAD..origin/main to see whose they are.
Is git push —force safe?
A plain --force overwrites the remote without checking anything. Use --force-with-lease --force-if-includes instead, and only on a branch nobody else pushes to. On a protected GitHub branch, force pushes are blocked by default (GitHub’s docs), unless the repo owner turned on “Allow force pushes”.
Why does git pull —allow-unrelated-histories fail?
On newer Git with no pull strategy set, git pull stops at “Need to specify how to reconcile divergent branches” before it tries the merge. Add --no-rebase to merge, or use git pull --rebase origin main, which worked in my test without the flag.
How do I fix “src refspec main does not match any”?
Make your first commit if the repository is new. If git branch --show-current prints a different name, like master, rename it with git branch -M main and push again.