This article discusses the error “src refspec master does not match any”, why this error occurs, what it means and how it can be resolved.
Causes of “src refspec master does not match any” error
Suppose we have a project folder: Project1. First, open the terminal and move into that folder.
cd Project1
Now, we will initialize a Git Repository:
git init
We also create a python file in folder Project1. It’s named main.py.
def welcome(city: str):
print(f“Welcome to {city}”)
if __name__ == ‘__main__’:
welcome(“HaNoi”)
Ok, now we have this file in our repository. Let’s create a link from the local repository (the version stored on your computer) to the remote repository (the version stored on GitHub) so that changes inside the project are easily updated.
git remote add origin https://github.com/MinhHoan147/Project1
git add .
git push -u origin master
After running the push command, you will get the error message
Output:
error: src refspec master does not match an
This error occurs because you forgot to commit before executing the push command, or maybe the master branch doesn’t exist.
To be clear, you can check the branches yourself by running the git branch command.
Solutions to this problem
To avoid errors, make sure you initiate a commit before pushing it.
git commit -m "Code optimization commit"
Then check the branch name to see if there is a master branch. Creating a master branch in local and remote repositories would be best. To make a remote master branch, you can access to GitHub website, or you can use these commands above:
git checkout -b master
git push origin master
Another tip is that instead of using the master branch, you can utilize the current default branch as the main branch.
Summary
So I helped you fix the “src refspec master does not match any” error. To avoid errors, pay attention to the order in which the steps are executed and the branch name.
Maybe you are interested:
- Fatal: detected dubious ownership in repository
- How to delete folder in Gitlab repository
- Git commit your changes or stash them before you can merge

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js