When you first try and push to a git repository, you may get the following error message:

1
2
error: src refspec master does not match any.
error: failed to push some refs to 'git@github ... .git'

This is because you have not committed your files!

How to Fix src refspec main does not match

1
2
git commit -m "initial commit"
git push origin main

The above code will commit your files that are staged, before pushing them to your desired branch.

By default, we push our code to the main branch above. Swap this out with another branch if you need.

How to Show all References in Git

If you want to first see the references in git, then you can use the show-ref command:

1
2
3
4
git show-ref

# abc123d5d8d40586df855302eca139ee2b75f789 refs/heads/main
# abc123d5d8d40586df855302eca139ee2b75f789 refs/remotes/origin/main

Once you know this, you can push the code accordingly:

1
git push origin HEAD:<branch>

You may also ask these questions

How do I fix error SRC Refspec master does not match any?

First check what git references:

1
git show-ref

How do I fix error SRC Refspec main does not match any error failed to push some refs to?

1
2
git commit -m "<your message>"
git push origin main

How do I push to main branch GitHub?

1
git push -u origin main

How do I force git to push?

1
git push -u origin <branch> -f