Skip to content
Start » git » How to resolve “error: src refspec main does not match any” in Git

How to resolve “error: src refspec main does not match any” in Git

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

A second line of message with the text: “error: failed to push some refs to ''” may also appear in the error stack.

Missing commit before push

The most common cause is the lack of files in the project track, that is, lack of git commit.

Yes, simple as that, when starting a project, and adding the files, if they are not committed before trying to send (git push) the same to the central repository, git reports missing refspec to the branch (main or master).

To solve then just follow the usual sequence:

git init git remote add origin [email protected]:nunomazer/my-repository.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 remote repository path
  3. Change the branch name to main (only required if using a version of Git that creates the main branch as master)
  4. Add the changed files
  5. Commit the files
  6. Finally the push (send) to the repository, now without errors

The main steps to avoid the error therefore are 4 and 5.

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 have been 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 the git push from an existing branch, or create the master (in this case) and then push it:

git checkout -b master git push -u origin master

If you know of other causes for the error “error: src refspec main does not match any” share it in the comments.

Leave a Reply

Your email address will not be published.

%d bloggers like this: