The error message: error: src refspec main does not match any happens for some common causes, most notably when starting a new repository for a project.

A second line of message with the text: error: failed to push some refs to github.com:nunomazer/my-repository.git may also appear in the stack error.

You can also get the error message: error: src refspec master does not match any, if your branch is master and not main.

This error is related to versioning your code with Git, and can happen if your project is in a managed repository on Github, Gitlab or Bitbucket.

What means the error: src refspec main does not match any

The most common cause for this error: src refspec main does not match any occurs is when there are missing files in the project track, ie missing execute the git commit command. You can even check on Github by clicking on commits if there are any made. Or by listing locally with the git log --oneline command.

Fixing the error: src refspec main does not match any

We need to understand the cause to fix the error related to src refspec not compatible with main or master branch. Next I show how to fix it depending of the cause.

Missing commit before push

Yes, simple like that, when starting a project, and adding the files, if they are not committed before trying to send (git push) them to the central repository on Github, git informs that refspec is missing for the branch (main or master).

So, to solve the error src refspec main does not match any just follow the usual steps sequence to init a repository:

git init
git remote add origin [email protected]:nunomazer/myrepository.git
git branch -M main
git add .
git commit -m 'feat: My first feature'
git push -u origin main

Following the example, considering a new repository, the steps were:

  1. Start the repository locally
  2. Add the remote repository path
  3. Change the branch name to main (only necessary if you are using a version of Git that creates the main branch as master)
  4. Add the changed files
  5. Commit the files
  6. Finally, push (send) to the remote repository on Github, “now without errors”

Attempting to push from one branch to another

The error message: “error: src refspec main does not match any” also happens if you are on a specific branch locally and try to push to another remote branch, which does not yet exist.

Something like:

git branch
* version-x  # you are in this branch
  version-y

git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'origin_address'

In the example above when calling git branch the version-x and version-y branches were listed, that is, they are the branches that exist locally.

When trying to push to master the error happens because the master branch does not exist locally.

So either you do a git push to an existing branch, or create the master branch (in this case) and then push to it:

git checkout -b master
git push -u origin master

What we’ve done here is to create a new branch called master with the git checkout -b command, and then push it to the central repository on Github (for example) with the git push command.

Conclusions

This error is caused by some contexts on you local repository: or you don`t have yet committed nothing or you are trying to push a branch that does not exists locally.

If you know any other causes for the error: src refspec main does not match any with GIT on Github or Gitlab, please share it in the comments.