Error src refspec head does not match any

Fix git error:src refspec origin does not match any error failed to push some refs to url.git possible solutions notes and fixes missing or skipping …
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.

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

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

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.

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

Понравилась статья? Поделить с друзьями:
  • Error src refspec development does not match any
  • Error src refspec develop does not match any
  • Error src refspec dev does not match any error failed to push some refs to
  • Error src refspec branch does not match any
  • Error squeezing flash