Fix "error: src refspec main does not match any" on git push
error: src refspec main does not match any
- error: src refspec master does not match any
- error: src refspec main does not match any error: failed to push some refs to 'https://github.com/user/repo.git'
- error: src refspec feature/login does not match any
- error: src refspec v1.0 matches more than one
The branch you want to push does not exist locally: either you have not made any commit yet, or your branch is called master, not main. Make a first commit, rename the branch with git branch -M main, then run git push -u origin main.
What this error means
In git push origin main, main is a "refspec": Git looks for a local reference (branch, tag) with that name to send. "src" means the source side, that is your local repository. If no reference matches, there is nothing to push. The most common case: after git init, the branch only really exists after the first commit; before that, git branch shows nothing. Another frequent case: Git creates master by default if init.defaultBranch is not set, while GitHub suggests git push -u origin main. The "matches more than one" variant means a branch and a tag share the same name.
Common causes
- No commit:
git initandgit addwere run, butgit commitwas forgotten or failed (user.name/user.emailnot configured, no file added). masterbranch instead ofmain(or the other way round): the local name does not match the one in the command.- Typo or case mismatch in the branch name, for example
feature/Logininstead offeature/login. - Branch that only exists on the remote: you see it on GitHub but never created or checked it out locally.
Step-by-step fix
-
List local branches and commits
If
git loganswers "does not have any commits yet", you must commit first. Otherwise, note the exact name of the current branch.git status git branch --show-current git branch -a git log --oneline -n 3 -
Make the first commit
Set your identity if Git asks for it ("Please tell me who you are"), add the files, then commit.
git config --global user.name "Your Name" git config --global user.email "[email protected]" git add . git commit -m "Initial commit" -
Rename the branch or push the right name
Rename the local branch to match the remote, or simply push the real name (
git push -u origin master).git branch -M main git push -u origin main -
Push the current branch without typing its name
HEADmeans the branch you are on: the command avoids any typo and creates the branch with the same name on the remote.git push -u origin HEAD -
Branch only on the remote
Fetch it and create the local branch that tracks it before working on it and pushing it.
git fetch origin git switch feature/login -
"matches more than one" variant
A branch and a tag share the same name: give the full reference type in the command.
git push origin refs/heads/v1.0 git push origin refs/tags/v1.0
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
- Set the default branch name once and for all:
git config --global init.defaultBranch main. - Use
git push -u origin HEADfor the first push of a branch: you cannot get the name wrong. - Avoid giving a branch and a tag the same name (prefix tags with
v, branches withfeature/).
Frequently asked questions
Why does git branch show nothing after git init?
A branch is a pointer to a commit. Until a commit exists, the branch is only a pending name: it appears after the first git commit.
Should I use main or master?
Both work. GitHub, GitLab and Bitbucket use main by default for new repositories; what matters is using the same name locally and on the remote.
Does git branch -M delete anything?
No, it renames the current branch and keeps all its commits. -M forces the rename if a local main branch already exists: check with git branch first.
Useful tools for this error
More Git & GitHub errors
- Fix "failed to push some refs" (! [rejected] fetch first) in Git
- 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