Git error: src refspec master does not match the solution. There are a few reasons Git throws an
error: src refspec master does not match any
. Let’s figure it out. This is a detailed article and I fully tried to cover all errors that I faced. And step by step I cover the fixing of src refspec master does not match any…
When git throw error: src refspec master does not match any?
Error type:
git push -u origin master
error: src refspec master does not match any.
$ git push origin master error src refspec master does not match any
error: Please make sure you have the correct access rights and the repository exists.
Setup: User name & Mail address
Setup these in git when you prepare
a new environment
.
Replace
<name>
with my name and
<email>
with my mail address.
Step 01: Check credentials first maybe you are using other Github account
Sometimes we use one or more git accounts so let’s check it. Go to windows search and type “Credential Manager”. Open Windows Credentials and check Github account details in Generic Credentials.
Windows OS: Credential Manager Windows
Credential Manager Github details.
Here we can see the username of the Github account. if the username is correct then proceed to the next step. Or if you have any doubt regarding the password then click Edit the and your Github account password and save it. Note: Restart your system (optional)
Step 02 Check Git remote origin
git init
git remote -v
The
-v flag
lets us see the URLs to which our repository is pointing:
git remote -v
If no line appears after running
git remote -v
then you first need to register git repo. Or if details are shown like in the above image then skip the next step.
This will change our pointer to the
reactjs-ecommerce-app
repository. And now we can change our repository and push our code using the
git push command
.
Case 01: Pushing the changes to the master or remote branch
Suppose you’ve created a repo & 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.
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.
git push -u origin master
error: src refspec master does not match any.
After adding
-u flag
a
Github Authentication POP-UP
would appear. Add Github username and password, or simply login using Token. If you choose Token then the pop-up will show a 6-digit code with a Github link. Copy the code and open the link where you need to add copied code and hit Authenticate.
Case 02: Check if a remote branch exists
Most developers replaced master branch with the main. So, the local branch and remote branch ref will differ, and when you try to push the changes,
Github will throw an error since the remote branch itself is not present
.
Solution: Case 02
First, 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>
git show-ref
This error occurred when we
use the wrong Github credentials
. Or
forgot to commit the changes
. Or 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. Or
typo in the branch name
while pushing the commit to the remote branch. Or there is no record of .git (fetch, push) when performing
git remote -v
. This article helps you to fix this error and you’ll successfully push code to the remote repository.