Git error: src refspec master does not match any

71 / 100

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.

git config --global user.name "<name>" && \
git config --global user.email "<email>"

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.

credential Manager Windows
Windows OS: Credential Manager Windows
credential Manager github details
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
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.

Add remote branch

git remote add origin /path/to/origin.git

e.g
git remote add origin https://github.com/devhassam/reactjs-ecommerce-app.git

Make sure to add .git not the URL path of github repository.

And check again remote origin entry using command git remote -v . hope all going well.

If the remote branch URL is wrong after running git remote -v

We can do this using the git remote set-url command:

git remote set-url origin https://github.com/devhassam/reactjs-ecommerce-app.git

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.

git remote add origin /path/to/origin.git
git add .

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.

Fixing: Case 01

Perform these commands.

git remote add origin /path/to/origin.git (skip if step-02 is passed)
git add .

git commit -m "First commit"
git push origin master

If the error is still there then just add <strong>-u flag</strong> with git push origin master

git remote add origin /path/to/origin.git (skip if step-02 is passed)
git add .

git commit -m "First commit"
git push origin -u master

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
git show-ref

Results 100% Working

New file created to test git push
New file created to test git push
commands to add, commit and push code
commands to add, commit and push code
File successfully added in git repo.
File successfully added in Github repo.

FAQ

Why Git error: src refspec master does not match any?

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.

Conclusion

The “Please make sure you have the correct access rights” error occurs if you do not have the right permissions to access a Git repository. Or error: src refspec master does not match any.

Read more articles:

Thank you! And I hope this article is helpful to fix the Git error: src refspec master does not match any.

Hassam
Hassam

I am passionate about open-source software, privacy, best practices, standards, user experience, and developer experience. My favorite stack these days are Laravel, Vue, and Tailwind. And focusing on Solid Principles, Data Structures, and Design Patterns.

Articles: 23

Leave a Reply

Your email address will not be published. Required fields are marked *