Skip to content
PC Moment
Programming

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

PC Moment title card for Error: src refspec main does not match any

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:

  1. No commits yet. git init creates an initial branch without any commits, and a branch with no commits has nothing to push. git status shows No commits yet, and git branch prints nothing.
  2. The branch name doesn’t match. git init still names the first branch master unless you’ve configured otherwise. The git docs say the default is “currently master, but this will change to main when Git 3.0 is released.” GitHub’s instructions push main. If your local branch is master, git push origin main has nothing to push.
  3. A typo or wrong case. git push origin mian or a branch called Main won’t match main.

Here’s how each cause shows up and what fixes it:

CauseHow you can tellFix
No commits yetgit status shows No commits yet; git branch prints nothingStage and commit your files (Step 2)
Branch is master, you push maingit status shows On branch mastergit branch -M main, or push master instead (Step 3)
Branch is main, you push mastergit status shows On branch mainPush main instead
Typo or wrong casegit branch shows a slightly different nameCopy 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. Run git add on your files first.

  • Git doesn’t know who you are. If you see Author identity unknown and *** 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.

  • fatal: not a git repository (or any of the parent directories): .git: you’re running the commands outside the project folder or before git init. See fatal: not a git repository.
  • no matching key exchange method found when 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 -v and check that origin points to the right repository.
  • Copy the branch name from git branch instead 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