error: src refspec master does not match any

There are quite a few reasons Git throws an error: src refspec master does not match any. Let’s look at each of these cases and their solutions.

When does git throws error: src refspec master does not match any?

Scenario 1 – Pushing the changes to master or remote branch

When you first time create a Git repository, the repository has no commit history. If you want to enforce a change in the repository, you must first make a commit.

mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .

After adding from the local branch files, 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.

Solution forerror: src refspec master does not match any.

All you need to perform is 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

Scenario 2 – Check if a remote branch exists.

If you are working on Github, it replaces the main branch with the main branch. That in these cases, the local branch and the distance distribution will be different, and if you try to enforce the changes, git will make a mistake because the remote branch itself will not.

Solution – 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>

Summery

It’s all About this issue. Hope all solutions helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Categories