How to Fix error: SRC Refspec master does not match any
How to Fix error: SRC Refspec master does not match any. Once you have created a commit, you can push it to a remote server. If you forget to create a commit and try to push your code to a remote server, Git will raise an error. In this article, we discuss what this error means and why it is raised. Kamerpower.com
When working with Git, you may come across an error that says “src refspace master does not match any”. Below, we will walk you through an example of this error so you can figure out how to fix it on your computer.
When does git throws error: src refspec master does not match any?
1. Checking if a remote branch exists [Example Scenario].
when working with Github, you will notice that they have replaced the master branch with the main branch. Which therefore mean that the local branch and remote branch ref will differ, and when you try to push the changes, git will throw an error since the remote branch itself is not present.
To fix the problem, you should check what refs you have, and once you find that, make a git push to the specific remote branch.
# To get all the
ref git show-ref
# replace with your branch name according to ref
git push origin HEAD:<branch>
2. Committing and pushing Empty Directory in Git gives an error
bitbucket does not track the empty directories for certain version of Git like GitHub, so if a directory is empty and you are trying to commit and push, it will lead to an error: src refspec master does not match any.
To fix the error, add a file to your directory before pushing it to a remote branch.
3. Mismatch in Local and remote branch [Example Scenario].
Even the typo in the branch name while pushing the commit to the remote branch will lead to a refspec error. In order to fix error, validate and check if you have given the right branch name while pushing the code to the remote branch.
Fixing error: SRC Refspec master does not match any when Pushing the changes to master or remote branch.
If you have created a git repository and added all the files from your local branch, but before committing the files, you try to push them into the remote branch or master branch.
mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .
git push -u origin master
error: src refspec master does not match any.
After adding the files from the local branch, if you do git push, you will get an error: src refspec master does not match any. error: failed to push some refs to master.
Solution for error: src refspec master does not match any.
Perform a git commit with a proper message and then do git push to the remote origin to avoid any errors.
mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .
git commit -m “initial commit”
git push origin master
Another case of Git error: src refspec master does not match any.
This error can occur for different reasons. The most likely reason this error will occur is that the master branch does not exist. Perhaps you cloned a new repository and the default branch is main, so there’s no master branch when you try to push for it.
Solution
Fixing the “src refspec master does not match any” Error. The solution to this error is to either create a local and remote master branch that you can push the commit to or to push the commit to an existing branch – maybe main.
You can create a remote master branch on a Git managed website (like GitHub) or you can do that directly from your terminal. The commands below will create a master branch locally. And by pushing to origin master, the master branch will also be created remotely.
git checkout -b master
# add commit
git push origin master
Conclusion
To conclude, if you get the Error: src refspec master does not match any error when you try to push to master, the most viable reason is that the masterbranch does not exist.
Recommendation