Your push failed because the branch you named doesn't exist locally yet: no commits, a master/main mix-up, or a typo. Here's how to tell which.
Adapted from the Git & GitHub Companion Guide.
You create a new repository on GitHub, copy the commands it shows you, and run them in your project folder. The last one fails:
$ git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/you/your-repo.git'
The message sounds like something is wrong with GitHub, your remote, or your permissions. It isn't. Nothing was sent anywhere. Git stopped before it ever contacted the remote, because the branch you asked it to push doesn't exist on your machine.
What's actually happening
A push command has two parts: what to push and where to put it. In git push origin main, Git treats main as shorthand for "take my local main and send it to main on the remote." That pairing is called a refspec, and the first half, the source, is the src in the error.
"src refspec main does not match any" means Git looked for a local branch or commit called main and found nothing. The word "any" is literal: it matches zero local references. The error is about your local repository, not the remote.
There are three common reasons your local main doesn't exist.
1. You haven't made a commit yet. A branch is only a name that points at a commit. In a brand-new repository with no commits, there's nothing for the name to point at, so the branch doesn't exist yet. You can see this: git status says "No commits yet", and git branch prints nothing at all. Staging files with git add isn't enough. Only a commit creates the branch.
2. Your branch has a different name. Older versions of Git, and many default setups, name the first branch master, not main. GitHub's instructions assume main. If you committed on master and then push main, Git has no local main to send.
3. The name is misspelled or has the wrong case. Branch names are case-sensitive. Main, MAIN and main are three different names, and git push origin Main fails with the same error.
The fix, step by step
1. Check which situation you're in. Run:
git status
git branch
git status shows the current branch name on its first line, and No commits yet if that applies. git branch lists your local branches with a * next to the current one. If the list is empty, you have no commits. If it shows master and you tried to push main, you have a naming mismatch.
2. If there are no commits, make one. Stage your files and commit:
git add .
git commit -m "Initial commit"
Now the branch exists. Run your push again.
3. If your branch is master and you want main, rename it. The -M flag renames the branch you're on, even if the new name is already taken:
git branch -M main
git push -u origin main
If you'd rather keep master, push the name you actually have: git push -u origin master.
4. If it's a typo or wrong case, retype it. Copy the exact name from the git branch output.
Two mistakes worth knowing about ahead of time
Re-running git remote add while troubleshooting. The remote is fine, and nothing in this error involves it. Running git remote add origin ... a second time just gives you error: remote origin already exists. If the URL really is wrong, git remote -v shows it and git remote set-url origin <url> fixes it.
Confusing this with "updates were rejected". They look similar because both appear after a push, but they mean different things. "src refspec does not match any" happens before contacting the remote, because your local branch is missing. "Updates were rejected" happens after contacting it, because the remote has commits you don't. If you see the second message, How to Fix 'Updates Were Rejected' covers it.
A habit that prevents the confusion entirely
Two small habits remove most of this. First, set Git's default branch name once, so every new repository starts on main and matches what GitHub expects:
git config --global init.defaultBranch main
Second, when you're not sure what a branch is called, don't type it. git push -u origin HEAD pushes whichever branch you're currently on to a branch of the same name on the remote. That sidesteps typos, case mistakes and the master/main mix-up in one command. And whenever you see "does not match any", run git branch first. It takes a second and tells you whether the branch you named exists at all.
If you'd like more posts like this sent straight to your inbox, subscribe to the newsletter.
Prefer to dig in yourself? The Git & GitHub repo on GitHub has more free examples and exercises.
Top comments (0)