Error src refspec branch does not match any

When working with Git, you may come across an error that says "src refspace master does not match any". Here's what the error means and how you can solve it. What Does src refspec master does not match any Mean in Git? You may get this error when you try to trigger a push from a local repository to a master repository like this: git push origin master This error can occur for different reasons. The most likely reason this error will occur is that the master branch does not exist. Perhaps

Error: src refspec master does not match any – How to Fix in Git

When working with Git, you may come across an error that says «src refspace master does not match any».

Here’s what the error means and how you can solve it.

You may get this error when you try to trigger a push from a local repository to a master repository like this:

git push origin master

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.

You can display the remote branches connected to a local repository using the git branch -b command like this:

git branch -b

# results
#  origin/main
#  origin/feat/authentication
#  origin/other branches ...

With the above results, you can see that there is no master repository (origin/master). So when you try to push to that repository, you will get the «respec error».

This result also applies to any other branch that does not exist. Let’s say, for example, I make changes and push to a remote hello branch that does not exist:

git add .
git commit -m "new changes"
git push origin hello

This command will produce the following error:

error: src refspec hello does not match any

How to Fix the «src refspec master does not match any» Error

Now you are aware that the master branch does not exist. 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 like this:

git checkout -b master

# add commit

git push origin master

These commands will create a master branch locally. And by pushing to origin master, the master branch will also be created remotely.

But if you do not want to create a master branch, you can use the existing default branch (which may be main) instead.

Wrapping up

So 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 master branch does not exist.



Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

fix git error:src refspec origin does not match any

This post talks about how to fix a git error.

error: src refspec master does not match any
error: failed to push some refs to ‘url.git’.

Possible causes for this error for the below use cases

  • Create and commit changes to the remote repository
  • Push the existing repository from the command line

Here are the steps and commands for creating and cloning and committing the repository

  • git init
  • git add README.md
  • git commit -m “Initial Changes”.
  • git remote add origin https://github.com/intkiran/angular-mock-api-json.git
  • git push -u origin master

error: src refspec master does not match any error: failed to push some refs to ‘url.git’

There are two use cases success and error flow.

Let’s see how we can reproduce these error use cases.

These are the not correct way of adding commits and pushing changes but given an example use case how we can reproduce this error?

  • I have a local project created angular-crud-mock-api in the b:\githubwork directory.

  • Create an empty repository in github.com my repository url.

https://github.com/intkiran/angular-mock-api-json.git
  • Now want to commit angular-curd-mock-api code changes into the Repository url
    existing local application created `angular-crud-mock-api which is not synced with the GitHub repository.
  • Let’s see how to add these changes to the remote repository
:githubworkangular-crud-mock-api>git remote add origin https://github.com/intkiran/angular-mock-api-json.git
  • Issue git push command and throws an error
B:githubworkangular-crud-mock-api>git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/intkiran/angular-mock-api-json.git'

That means, we have to add the first files or directory before pushing changes.

  • Add the files and directories using the below command git add .
B:githubworkangular-crud-mock-api>git add .
warning: LF will be replaced by CRLF in angular-crud-mock-api/.browserslistrc.
The file will have its original line endings in your working directory.

It adds the changes to the local repository in git.

Now, Let’s try to push changes to the remote repository using the git push command.

  • Next, push changes using the git push command.
B:githubworkangular-crud-mock-api>git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/intkiran/angular-mock-api-json.git'

This error throws an error and you need to use the commit command before pushing the Command.

  • commit changes
    Here are the committed changes to the local repository.
B:githubworkangular-crud-mock-api>git commit -m "Initial changes"
[master (root-commit) 96c6c0c] Initial changes
 29 files changed, 30724 insertions(+)

How to add add,commit push Success Flow in Github

Let’s see how we can reproduce these success use cases.

Here is a sequence of commands you need to run to avoid the error.

git remote add origin https://github.com/intkiran/angular-mock-api-json.git
git add .
git commit -m "Initial changes".
git push -u origin master

Here is the output of push changes

B:githubworkangular-crud-mock-api>git push -u origin master
Enumerating objects: 38, done.
Counting objects: 100% (38/38), done.
Delta compression using up to 4 threads
Compressing objects: 100% (34/34), done.
Writing objects: 100% (38/38), 260.09 KiB | 5.20 MiB/s, done.
Total 38 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/intkiran/angular-mock-api-json.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

fix git error:src refspec origin does not match any

You have to carefully check the below things to avoid this error, if you create new repo or push an existing repository,

  • You forgot to add the files before pushing changes to the master or branch

  • Missing or skipping the git add .or git commit command throws an error

Here are steps for adding the files or directories

  • git commit message enclosed in double quotes instead of single quotes

Valid

git commit -m "initial changes"

Invalid

git commit -m 'initial commit'
  • Please check the branch name with the push command

Usually, we will push changes using the below command.

git push -U origin `branchame`

push command push changes from the local repo to the remote repository branchname
Please make sure that branchname exists.

‘master ‘ is the default branch.

Here is a correct command

git push -U origin master

Possible reasons

  • Branch name not found
  • Branch name spelling mistake or case insensitive

How to create a new repo to avoid this error in Git

here is a list of commands to avoid this error

git init
git add .
git commit -m 'message'
git push -u origin master

Conclusion

In this tutorial, Learn how to fix the git error:src refspec origin does not match any while working with git repositories.

Я создал новый проект с одним файлом README.md.
И сделал команду

git clone https://github.com/xxxx/xxx.git
git add .
git commit -m "v.01"
git push origin master

Но получаю ошибку

error: src refspec master does not match any
error: failed to push some refs to https://github.com/xxxx/xxx.git

Я вводил
git show-ref

3deeeb53dda70bea0809cff5e4032011ba45ac7d refs/heads/main
27383695f3f76e87254687449d73250254560bbb refs/remotes/origin/HEAD
27383695f3f76e87254687449d73250254560bbb refs/remotes/origin/main
3deeeb53dda70bea0809cff5e4032011ba45ac7d refs/remotes/origin/master

И делал так

Maybe you just need to commit. I ran into this when I did:

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

Oops! Never committed!

git push -u origin master
error: src refspec master does not match any.

All I had to do was:

git commit -m «initial commit»
git push origin master

Success!

Я пытался еще так
git push origin HEAD:master
Но хоть загрузка и произошла но файлы не загрузились в github

Но это не помогло что мне делать как исправить


  • Вопрос задан

    более двух лет назад

  • 18021 просмотр

Нет ветки master, вот и ругается. Ветка по умолчанию на GitHub теперь называется main.

Команда git branch -vv покажет какие ветки есть локально и с какими внешними ветками связаны.
* main 0e02250 [origin/main] v.01

Надо было делать git push origin main
Либо просто git push т. е. отправить текущую ветку в связанную с ней ветку на внешнем репозитории.
В нашем случае текущая ветка main (помеченная звёздочкой)
отслеживает исходную ветку main в репозитории обозначенном как origin

Что скрывается за сокращением origin покажет команда git remote -v

origin	https://github.com/xxx/xxx.git (fetch)
origin	https://github.com/xxx/xxx.git (push)

Пригласить эксперта

Ввожу git branch -vv — никакого ответа
git push -u origin main не работает хотя связь с удаленным репозиторием установлена

работает так
Для отправки в удаленный репозиторий
git push origin master:master


  • Показать ещё
    Загружается…

13 февр. 2023, в 17:43

3000 руб./за проект

13 февр. 2023, в 16:58

25000 руб./за проект

13 февр. 2023, в 16:52

5000 руб./за проект

Минуточку внимания

Table of Contents
Hide
  1. When does git throws error: src refspec master does not match any?
    1. Scenario 1 – Pushing the changes to master or remote branch
    2. Solution for error: src refspec master does not match any.
    3. Scenario 2 – Check if a remote branch exists.
    4. Scenario 3 – Mismatch in Local and remote branch
    5. Scenario 4 – Committing and pushing Empty Directory in Git

There are quite a few reasons Git throws an error: src refspec master does not match any. Let us look at each of these cases and the solution to it.

Scenario 1 – Pushing the changes to master or remote branch

Let’s say 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 .

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.

Solution for error: 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 with Github, they have replaced the master branch with the main branch. Hence, in these circumstances, 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.

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>

Scenario 3 – Mismatch in Local and remote branch

Generally, even the typo in the branch name while pushing the commit to the remote branch will lead to a refspec error. 

Solution  Validate and check if you have given the right branch name while pushing the code to the remote branch.

Scenario 4 – Committing and pushing Empty Directory in Git

A certain version of Git like GitHub, bitbucket does not track the empty directories, 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.

Solution – Add a file to your directory before pushing it to a remote branch. 

Ezoic

Avatar Of Srinivas Ramakrishna

Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.

Did you try to push changes to master with the following?

$ git push origin master

But received an error that says:

error: src refspec master does not match any

The most common reason for this is that “master” isn’t called “master” anymore. To fix the issue, replace “master” with “main“.

$ git push origin main

Didn’t help?

This is a comprehensive guide to fixing the “error: src refspec master does not match any” -error. You will find easy fixes with explanations as to what’s going wrong.

Reasons for the “src refspec does not match any” -Error

Let’s have a closer look at the problems that might be causing the src refspec error.

1. The “master” Branch Isn’t Called “master”

Recently, Git replaced the name “master” with “main”. This means the default branch of your project is no longer called “master” by default but “main” instead.

Pushing changes to the non-existent “master” branch will cause issues. This is one of the most common explanations as to why you might see “error: src refspec master does not match any” when pushing.

In this case, you can try pushing to “main” instead.

$ git push origin main

If this doesn’t fix the issue, your default branch might have a different name than “main” or “master“.

To figure out what the “master” or “main” is called in your case, run the following:

$ git show-ref

The default branch is one of these references. Pick the one that’s your default branch and push the changes to it.

2. You Forgot to Commit

Another common reason why you might get the “error: src refspec master does not match any” error when pushing in Git is you haven’t made a commit.

For example, let’s start by creating an example repository and try to push it to GitHub:

$ mkdir example
$ cd example
$ echo "# Just another github repo" >> README.md
$ git init
$ git add README.md
$ git remote add origin https://github.com/user/repo.git
$ git push -u origin main

When running these commands, you will see an error:

error: src refspec main does not match any

This happens because you didn’t commit anything to the repository yet. In technical terms, a branch doesn’t exist before there’s at least one commit in the repository.

So make sure you’ve committed the changes before trying to push!

For instance, in the above, we forgot to commit the new README.md file after adding it. To fix this, create a commit and push again:

$ git commit -m "Initial commit"

Summary

The most common reason for the “error: src refspec master does not match any” -error is that you’re trying to push to “master” which these days is called “main“. In other words, you’re trying to push to a branch that doesn’t exist.

Another reason this error might occur is that your branch is empty and doesn’t exist. This can happen if you’ve initialized your repo, and added changes with git add but forgot to commit the changes with git commit. Before the initial commit, the branch doesn’t technically exist, and pushing will fail!

Thanks for reading. Happy coding!

About the Author

I’m an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.

Recent Posts

As you already know git is an open and free version control system that allows you to handle small to large projects in an efficient way. But while making changes to the file locally or server you will generally face errors like the error: src refspec master does not match any. If you are getting this type of error then you have come to the right place. In this entire tutorial, you will different scenarios where you will get this type of error and how you can solve it using the provided solution.

Before going to different scenarios let’s first create a sample project folder and initialize git for it. Make sure you must have git installed in your system.

Execute the following line of code to initialize the git for the project folder.

Create a Sample Folder

mkdir sample-project

Go to the created folder

cd sample-project

Initialize the git for the current folder

git init

Initializing version control for Sample Project Folder

Initializing version control for Sample Project Folder

Different Scenarios when Git throw error :src refspec master does not match any error

Case 1: Pushing the empty directory

The first case for getting this error: src refspec master does not match any is when there is nothing in your directory and you are pushing the changes to the remote branch.

Solution

The solution to this error is very simple to try to first create a file inside that directory and then commit and push to the remote server.

Case 2:  Push to the master branch before committing

The second case when this error came is when you are pushing the changes directory to the master branch.

Solution 

The solution for this case is that you should commit all changes and then push to the master branch. You will not get any errors.

Case 3: No branch in remote exists and getting the error src refspec master does not match any error

The third case for getting this error is when there is no branch in the remote with the same name in your local host. For example, if I push the changes to the master branch then I will get this error. Because there is no master branch in the remote.

master branch doesnot exist

master branch does not exist

Solution

The solution of the case is to first find all the references to the branches and then push to that remote branch. To get the references you have to use

git show-ref

References for all the branch

References for all the branch

You can see I was pushing to the master branch but in the remote the branch name was main. So to remove the error src refspec master does not match any error, push changes to the main branch.

Pushing the changes to the main branch

Pushing the changes to the main branch

Case 4: Not matching the name of local and remote branch

If your local branch name is different from the remote branch name then you will get this type of error. For example, my remote branch name is main and but I am trying to push the changes to main branch.

Name mismatch for branch name

Name mismatch for branch name

Solution 

The solution for this case is very simple. Check the name of the local and remote branches before pushing the changes. You will not see any errors.

Main branch

Main branch

Conclusion

These are the different scenarios when you can get git error “error: src refspec master does not match any“. If you carefully apply the solution then this error will be solved. The most common error comes from case 4. I hope you have liked this tutorial. If you still facing this error then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Git throws error: src refspec master does not match any, due to a number of reasons and most common are – Wrong or spelling mistake in branch name, using older ‘master’ instead of ‘main’, or forgot commit before pushing.

Steps to follow

1. Commit before pushing to branch

mkdir newrepo && cd newrepo
git remote add origin /path/to/origin.git
git add .
git push -u origin master
error: src refspec master does not match any.

Here we forgot to commit before pushing. So, do it like this –

mkdir newrepo && cd newrepo
git remote add origin /path/to/origin.git
git add .
git commit -m "my first commit"
git push -u origin master

2. Windows expect commit messages to be in double quotes.

git commit -m 'my first commit'

Sometimes, git throws error on this. You need to enclose the message in double quotes.

git commit -m "my first commit"

3. Check if you are committing empty directory

In some versions of git or depending on platforms like github or bitbucket, empty directories do not commit. They might not throw an error during this step but while pushing they might do. To solve this problem, you may add a file to the directory and then push.

4. Check your local and remote branch names

Generally, a small spelling mistake could lead to problems. Suppose your branch name is school and you are using below command then it will throw refspec error –

git push scool

5. Check if branch exists

There are situations when people read outdated documentation and got into problems. One of such problem is ‘master‘ branch. Github replace it with main branch. So, you need to check if master branch exists.

git push origin main

6. Check for what refs you have

It is important to check if wherever you are pushing your code, that ref exists or not. To check that you can use this command –

git show-ref

This command will return something like this – refs/heads/master or refs/heads/main.

Now you can try pushing using this command –

git push origin HEAD:main

    Tweet this to help others

This is Akash Mittal, an overall computer scientist. He is in software development from more than 10 years and worked on technologies like ReactJS, React Native, Php, JS, Golang, Java, Android etc. Being a die hard animal lover is the only trait, he is proud of.

Related Tags
  • Error,
  • git error,
  • git short

In this article, we will see how to solve "error: src refspec master does not match any" if you are also getting this error during git push operation. Last night when I was trying to push my release branch changes to the master, I noticed that it was failing with the "error: src refspec master does not match any".  While I  wasn’t expecting this error to come but this is something which can occur at any time if you miss something. So at this time I decided to write an article about this so that it will help you guys also in case you are also getting this error.

But before proceeding towards the solution, let me tell you this error can be solved in multiple ways depending on your scenario and the branching strategy that you follow. Here I will explain you the best method which you can use even in a production or in a critical system with full confidence. This is fully tested and generally works fine in all Git based source control including Bitbucket.

Solved: "error: src refspec master does not match any" when using git push

Also Read: How to Create and Work on your Own Bitbucket Feature Branch

To explain the scenario in my system, I am currently having two local branches — develop and release/1.0.1 as you can see below. Here after adding all the changes to my release/1.0.1 branch, I am trying to push it to the remote master.

cyberithub@ubuntu:~$ git branch
  develop 
* release/1.0.1

From the release/1.0.1 branch, I tried to push the changes using git push -u origin master command then suddenly I noticed that it is failing with below error.

cyberithub@ubuntu:~$ git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://app.cyberithub.com/bitbucket/scm/application/example-app.git'

While the above error could occur due to many reasons but for me it occurs because I was pushing the changes to master branch from a source branch which did not had any reference to the remote master branch. So to fix the above error, first I had to create and switch to new local master branch using git checkout -b master command as shown below.

cyberithub@ubuntu:~$ git checkout -b master
Switched to a new branch 'master'

You can verify the current branch by using git branch command as shown below.

cyberithub@ubuntu:~$ git branch
  develop
  release/1.0.1
* master

Here first I needed to pull all the changes from remote master branch using git pull origin master command as shown below.

cyberithub@ubuntu:~$ git pull origin master
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

From https://app.cyberithub.com/bitbucket/scm/application/example-app.git
* branch          master     -> FETCH_HEAD
Already up to date!
Merge made by the 'recursive' strategy.

Once the branch is in sync with remote master, I pulled all the files from my release branch using git pull origin release/1.0.1 command as shown below.

cyberithub@ubuntu:~$ git pull origin release/1.0.1
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

From https://app.cyberithub.com/bitbucket/scm/application/example-app.git
* branch           release/1.0.1 -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 config/test.yaml                     | 2 +-
 config/hello.yaml                    | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

After merging all the release changes to master, I again tried to push the changes to remote master branch by using git push -u origin master command as shown below.

cyberithub@ubuntu:~$ git push -u origin master
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 565 bytes | 565.00 KiB/s, done.
Total 2 (delta 1), reused 0(delta 0), pack-reused 0
remote: Checking connectivity: 2, done.
remote:
remote: Create pull request for master:
remote:   https://app.cyberithub.com/bitbucket/scm/projects/EXAMPLE/repos/example-app/pull-requests?create&sourceBranch=refs/heads/master
remote: 
To https://app.cyberithub.com/bitbucket/scm/application/example-app.git
   768ca03..9acb8ca  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

As you can see from the above output, this time git push to remote master branch worked successfully. So this is how I solved my error. But this is not always the case with everyone. You might be getting "error: src refspec master does not match any" due to some other reasons like you might not have done the initial commit inside the repository and without that you are trying to push the changes. In that case also you will get the same error. So to fix this kind of error you need to first do the initial commit in your local repo and then only push the changes using git push -u origin master command as shown below.

touch somefile
git add somefile
git commit -m "Initial Commit"
git push -u origin master

Hopefully the above solution should be enough to solve your "error: src refspec master does not match any" too. Please let me know your feedback on the comment box.

Понравилась статья? Поделить с друзьями:
  • Error squeezing flash
  • Error sqlstate hy000 2002 connection refused
  • Error setfullscreen creating the device failed max payne
  • Error service unavailable message service unavailable
  • Error send key трофейная рыбалка