Error src refspec refs heads master 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

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

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

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

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


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

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

  • 17866 просмотров

Нет ветки 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


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

09 февр. 2023, в 18:09

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

09 февр. 2023, в 18:08

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

09 февр. 2023, в 17:54

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

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

Error Description:

  • It happens when we try to checkout/clone the new repository from local machine
$ mkdir carboncake
$ cd carboncake
$ git init 
$ touch a_text_file.txt 
$ git add a_text_file.txt 
$ git remote add origin [email protected]:repositories/carboncake.git
$ git push origin master
  • It causes an error
error: src refspec master does not match any.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to '[email protected]:repositories/carboncake.git'

Solution 1:

You’ve created a new repository and added some files to the index, However you haven’t created your first commit yet:

 git commit -m "Initial commit."

Solution 2:

  • The origin has no master branch,when you clone an empty git repository, So the first time you have a commit to push:
  • It will create a new master branch for you.
  • If this didn’t fix your issue then it is an gitolite-related issue:
  • An example conf file that came with your gitolite. It looks like below code:
repo    phonegap                                                                                                                                                                           
    RW+     =   myusername otherusername                                                                                                                                               

repo    gitolite-admin                                                                                                                                                                         
    RW+     =   myusername                                                                                                                                                               
  • Make sure you’re setting your conf file correctly.
  • Gitolite actually replaces the gitolite user’s account with a modified shell that doesn’t accept interactive terminal sessions. You can see if gitolite is working by trying to ssh into your box using the gitolite user account.
  • Whether it knows who you are it will say something like «Hi XYZ, you have access to the following repositories: X, Y, Z» and then close the connection. If it doesn’t know you, it will close the connection.
  • Finally, after your first git push failed on your local machine you should never resort to creating the repo manually on the server.First we need to know why your git push failed .There will be more confusion when you don’t use gitolite exclusively.

Solution 3:

  • While adding an empty directory. Git doesn’t allow to push empty directory. Here is a simple solution.

    Create the file .gitkeep inside of directory you want to push to remote and commit the «empty» directory from the command line:

touch your-directory/.gitkeep
git add your-directory/.gitkeep
git commit -m "Add empty directory"

UP NEXT IN GIT

  • Git Tutorial
  • Git Interview Questions and Answers
  • Git HR Interview Questions and Answers
  • Git Sample Resume
  • Git Interview Dress Code
  • Git Job Apply Letters
  • Git Forums

Git is a great tool for software developers. Being able to manage code repositories and versioning with the ease it provides can be a lifesaver in a ton of situations. 

However, that’s not to say that Git is flawless and, well, easy to use. A lot of times, you’ll see an utterly perplexing error that you never expect. The good news is, solutions are more often than not simple.

In this article, we’re going over the “src refspec master does not match any” error in Git and listing down solutions you can try out. 

Also read: How to remove untracked files in Git?


Be sure to commit before pushing to a branch

One of the most popular reasons you might see this error is because you might’ve forgotten to commit before pushing your code to a branch or even a new repository, for that matter. Make sure to commit to the branch (or repo) before pushing, and you’ll be good to go.

git commit -m "Commit Name here"

Commit the right way

Another time Git might throw this error your way is when you make a mistake during commit, specifically, using single quotes instead of double quotes. So instead of doing this. 

git commit -m 'Single Quotes'

Try this.

git commit -m "Double Quotes"

Also read: How to fix Gitignore not working issue?


Check the commit directory

Some platforms like GitHub and BitBucket don’t allow users to commit to an empty directory. If you’re using these platforms, make sure to add a file or two before you start pushing your code. 


Check branch names

Using incorrect branch names can also cause this error a lot of times. It’s not uncommon to make typos, so make sure you’ve got the branch’s name right, whether it’s a local or remote branch. 

This can also be extended to whether or not a branch actually exists before you start pushing. Check to make sure you’ve got the name right, the directory isn’t empty and that the branch actually exists.


Check for refs

Another thing you should check is if that your ref actually exists or not. You can do this using the following command.

git show-ref

The command will output something like refs/heads/master or refs/heads/main. What you’re looking for here is the last part of the output. Now try pushing as follows.

git push origin HEAD:main

Also read: How to fix “Error: could not find or load main class” in Java?

Yadullah Abidi

Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars.

You can contact him here: [email protected]

Понравилась статья? Поделить с друзьями:
  • Error src refspec refs heads main does not match any
  • Error src refspec master does not match any как исправить
  • Error src refspec master does not match any heroku
  • Error spawn e2big
  • Error sparkcontext error initializing sparkcontext