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 may also appear in the stack error.

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.

Missing commit before push#

The most common cause 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.

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:

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

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.

Se você conhece outras causas para o erro error: src refspec main does not match any com GIT no Github ou Gitlab, compartilha nos comentários.

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.