Error src refspec master does not match any error failed to push some refs to

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.

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.

Я создал новый проект с одним файлом 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 руб./за проект

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

You need to add a file to a commit before you can push your changes to a remote Git repository. If you create a new repository and forget to add a file to a commit, you may encounter the “src refspec master does not match any” error.

In this guide, we discuss what this error means and why it is raised. We walk through an example of this error so you can figure out how to fix it on your computer.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest

First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

src refspec master does not match any

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

The workflow for pushing a change to a repository looks like this:

  1. Change a file
  2. Add the file to the staging area
  3. Create a commit

Once you have created a commit, you can push it to a remote server. If you forget the third step and try to push your code to a remote server, Git will raise an error. This is because Git will be unsure about what changes need to be made to the remote repository.

An Example Scenario

We’re going to create a Git repository for a new HTML project. To start, let’s create the directory structure for our project:

mkdir html-project
cd html-project

We have created a directory called html-project and then we have moved into that directory.

Now that we have our folder ready, we can initialize a Git repository:

This command creates a hidden folder called .git/ which contains the configuration for our repository. Next, we create our first project file. We’re going to call this file index.html and add the following contents:

This file only contains one tag because we are still setting up our project. Now that we have a file in our repository, we’re going to link it up to a remote repository.

Our remote repository is hosted on GitHub. This will let us keep track of our project using the GitHub platform. To connect our local repository to the GitHub repository, we must add a remote reference to the GitHub repository:

git remote add origin https://github.com/career-karma-tutorials/html-project

After running this command, Git will know where our commits should go when we push them to our remote repository. Now we can add our changed file to our project:

Our index.html file is now in the staging area. To display this file on our remote repository, we can push it to the origin repository we just defined:

git push -u origin master

Let’s see what happens when we run this command:

error: src refspec master does not match any.

An error is returned.

The Solution

The git add command does not create a commit. The git add command moves files to the staging area. This is a triage space where files go before they are added to a commit. You can remove and add files from the staging area whenever you want.

This error is common if you try to push changes to a Git ref before you have created your first commit to your local repo or remote repo.

We need to create an initial commit before we push our code to our remote repository:

git commit -m "feat: Create index.html"

This will create a record of the repository at the current point in time, reflecting all the changes we added to the staging area. Now, let’s try to push our code.

Our code is successfully pushed to our remote repository.

Conclusion

The “src refspec master does not match any” error occurs if you have forgotten to add the files you have changed to a commit and try to push those changes to a remote repository before you make the first commit in your repository.

To solve this error, create a commit using the git commit command and then try to push your changes to the remote repository. Now you have the knowledge you need to fix this error like a professional coder!

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 src refspec main ничему не соответствует
  • Error src refspec main does not match any что это
  • Error src refspec main does not match any что делать
  • Error src refspec main does not match any ошибка
  • Error src refspec main does not match any как исправить