Error: src refspec main does not match any (and master): How to Fix It
Git can't push main or master because that branch doesn't exist locally yet. Make your first commit, match the branch name, and push again.
By PC MomentUpdated 5 min read

You set up a new repository, added the remote, ran git push -u origin main, and Git answered:
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/you/repo.git'
Or the same thing with master:
error: src refspec master does not match any
Git is telling you there’s no local branch with that name to push. Usually that’s because you haven’t made a commit yet, or because your branch is called master and you’re pushing main (or the other way around). Make a commit, push the branch name you actually have, and the error goes away.
What causes “src refspec main does not match any”
In git push origin main, main is a refspec. The part before an optional colon is the source (src), which the git manual describes as “often the name of the local branch to push.” Git looks for a local branch with that name. If it can’t find one, there’s nothing to send and it stops with this error.
There are three common reasons:
- No commits yet.
git initcreates an initial branch without any commits, and a branch with no commits has nothing to push.git statusshowsNo commits yet, andgit branchprints nothing. - The branch name doesn’t match.
git initstill names the first branchmasterunless you’ve configured otherwise. The git docs say the default is “currentlymaster, but this will change tomainwhen Git 3.0 is released.” GitHub’s instructions pushmain. If your local branch ismaster,git push origin mainhas nothing to push. - A typo or wrong case.
git push origin mianor a branch calledMainwon’t matchmain.
Here’s how each cause shows up and what fixes it:
| Cause | How you can tell | Fix |
|---|---|---|
| No commits yet | git status shows No commits yet; git branch prints nothing | Stage and commit your files (Step 2) |
Branch is master, you push main | git status shows On branch master | git branch -M main, or push master instead (Step 3) |
Branch is main, you push master | git status shows On branch main | Push main instead |
| Typo or wrong case | git branch shows a slightly different name | Copy the exact name from git branch |
How to fix it
Step 1: Check your branch and commits
git status
The first line shows your current branch, for example On branch master. If you also see No commits yet, go to Step 2. If not, skip to Step 3.
git branch
This lists your local branches and marks the current one with *. In a repository with no commits, it lists nothing.
Step 2: Make your first commit
Stage your files and commit them:
git add .
git commit -m "First commit"
git add . stages everything in the current folder, and git commit records it as the first commit, which also creates the branch.
Watch the output of git commit. Two problems often stop the first commit without you noticing:
-
Nothing was staged. If you see
nothing added to commit but untracked files present (use "git add" to track), the commit didn’t happen. Rungit addon your files first. -
Git doesn’t know who you are. If you see
Author identity unknownand*** Please tell me who you are., set your name and email, then run the commit again:git config --global user.name "Your Name" git config --global user.email "[email protected]"These save your identity for every repository on this computer.
Run git log --oneline to confirm the commit exists.
Step 3: Match the branch name
Now make the name you push match the branch you have. Pick one of these.
Option A: Rename your branch to main. This is what GitHub’s setup instructions expect:
git branch -M main
This renames the current branch to main. -M is short for --move --force, so it renames even if a branch called main already exists, replacing it. If you’d rather Git stop in that case, use -m instead.
Option B: Push the branch name you already have. If your remote uses master, or you simply want to keep the name:
git push -u origin master
Option C: Push your local branch to a differently named remote branch. The refspec format <src>:<dst> lets you push local master to remote main without renaming anything locally:
git push -u origin master:main
Options A or B are usually less confusing in the long run, because your local and remote branch names stay the same.
Step 4: Push and set the upstream
git push -u origin main
This pushes main to the remote called origin and, thanks to -u, sets origin/main as its upstream. You’ll see branch 'main' set up to track 'origin/main'. From now on, plain git push and git pull work. If you later see The current branch has no upstream branch, our guide to git push set upstream explains the fix.
Prevent it next time
Tell Git to name the first branch main in every new repository:
git config --global init.defaultBranch main
This works in Git 2.28 and later and only affects repositories you create after setting it. Check your version with git --version.
You can also choose the name for a single repository when you create it:
git init -b main
GitHub’s command-line instructions for adding an existing project use exactly this sequence:
git init -b main
git add .
git commit -m "First commit"
git remote add origin REMOTE-URL
git push -u origin main
Replace REMOTE-URL with your repository’s URL. GitHub notes that -b needs Git 2.28.0 or later.
Related errors
fatal: not a git repository (or any of the parent directories): .git: you’re running the commands outside the project folder or beforegit init. See fatal: not a git repository.no matching key exchange method foundwhen you push over SSH: the problem is the SSH connection, not the branch. See SSH KexAlgorithms.! [rejected] ... (fetch first): the remote already has commits, for example a README created on GitHub. Pull and merge them first. Only replace them if you’re sure you don’t need them, and read git push —force-with-lease before you do.- You committed the wrong files in that first commit: see git undo last commit.
If it still doesn’t work
- Run
git branch -a. It lists local branches and the remote-tracking branches Git knows about, so you can compare names exactly, including uppercase and lowercase. - Run
git remote -vand check thatoriginpoints to the right repository. - Copy the branch name from
git branchinstead of typing it, to rule out typos. - If the error names a tag or a branch with a slash, like
feature/login, push exactly that name:git push -u origin feature/login.
For more Git troubleshooting, see the programming guides.
Frequently asked questions
What does src refspec main does not match any mean?
Git couldn't find a local branch called main to push. Either the repository has no commits yet, so no branch exists, or your branch has a different name, such as master.
Is the fix the same for src refspec master does not match any?
Yes. It's the same error with a different branch name. Make sure you have at least one commit and push the branch name you actually have, or rename it to match.
Should my branch be called main or master?
Either works with Git. Use whatever your remote expects. GitHub's instructions use main, and git init still uses master unless you set init.defaultBranch or use git init -b main.
How do I make new repositories use main automatically?
Run git config --global init.defaultBranch main. It works in Git 2.28 and later and affects repositories you create from then on.
Related guides

Programming
Git Push Set Upstream: Fix "The Current Branch Has No Upstream Branch"
Fix "fatal: The current branch has no upstream branch" with git push -u, set tracking on an existing branch, and make Git set the upstream for you.
5 min read

Programming
Git Push --force-with-lease: The Safer Way to Force Push
What git push --force-with-lease does, how it differs from --force, when to use it after a rebase or amend, and why --force-if-includes makes it safer.
7 min read

Programming
Fatal: Not a Git Repository: What It Means and How to Fix It
Git says "fatal: not a git repository" when it can't find a .git folder. Here's how to find the right folder, start a repository, and fix GIT_DIR, submodule and ownership problems.
7 min read

Programming
Git Undo Last Commit: Reset, Revert, Amend and How to Get It Back
Undo your last Git commit the right way: keep the changes, throw them away, or reverse a commit you already pushed, plus how to recover from a bad reset.
7 min read