Src refspec dev does not match any ошибка

I have a project with a few friends in GitLab, and there is of course the master branch, and there are some others too. When I cloned the repository, I created also an upstream with the command git remote add upstream ....

Then, I issued the git fetch upstream. Followed by git checkout upstream/test1. Now, if I type git branch -a, I get an output like this:

* (HEAD detached at upstream/test1)
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/upstream/test1
  remotes/upstream/master

This is all fine, but then I did some changes to the code in my upstream/test1 branch, and I want to push them to origin/test1 repository, I get the error message on the title. Please note that I follow the steps below to push:

git add .
git commit -m "Sample message"
git push -u origin test1

If I issue git show-ref, I get the following output:

refs/heads/master
refs/remotes/origin/HEAD
refs/remotes/origin/master
refs/remotes/upstream/test1
refs/remotes/upstream/master

I checked the following questions, but didn’t find it helpful. Any ideas how to solve it?

1. Overview

Working with Git is an essential part of any developer’s day-to-day work. However, in the beginning, it could be overwhelming, and error messages might not be obvious. One of the most common issues people receive when starting working with Git is the error with refspec:

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

In this tutorial, we’ll learn the reasons for this issue and how to resolve and mitigate it.

2. The Description of a Problem

Many of us have seen the refspec error message at least once within the console. This error occurs on pushing to a remote repository. Let’s try to understand what this line exactly means:

error: src refspec master does not match any

Simply put, this error message tells us that we don’t have a branch we want to push, which is the main reason for this error.

3. Going Through the Steps

The refspec error might appear when we cloned an uninitialized repository and tried to push a local repository. This is how Git services explain setting up a local repository. Here are the steps from GitHub:

$ echo "# repository" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin https://github.com/profile/repository.git
$ git push -u origin main

We’ll refer to these steps in the paragraphs below.

4. Pushing a Non-existent Branch

Let’s go step by step through the instruction GitHub provides us.

4.1. Initializing a Repository

The first line creates a README.md file:

$ echo "# repository" >> README.md

The following command will initialize a local Git repository:

$ git init

This command may issue the following message:

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>

In 2020 GitHub changed the default name of the branch created in a new repository from “master” to “main.” The same changes have happened on GitLab. It’s still configurable on GitHub, GitLab, and Git.

4.2. The First Commit

The subsequent two commands create a commit in our local repository:

$ git add README.md 
$ git commit -m "first commit"

4.3. Renaming a Branch

The interesting thing happens in the following line:

$ git branch -M main

This line is responsible for renaming our current local branch to “main.” This happens because of the reason explained in the hint message. This line will rename our default branch to match the default branch name on our remote repository.

The steps provided by GitHub will contain the default name configured on the platform. However, this remaining became one of the most common reasons behind the refspec error. Let’s see what will happen if we have a local repository that uses “master” as a default branch and a remote which uses “main.” We’ll skip the renaming step for this example and go straight to setting up our remote repository:

$ git remote add origin https://github.com/profile/repository.git

4.4. The Problem

We’ll start experiencing problems on this line:

$ git push -u origin main

Let’s review this line and consult the documentation to understand what’s happening. This line pushes the changes from a branch, in this case, “main,” to a remote repository we configured in the previous line.

That means that the local repository should contain the “main” branch. However, the default local branch name is set to “master,” and we didn’t create a newmain branch or rename the “master” branch. In this case, Git won’t be able to find themain branch to push, and we’ll get this error message:

error: src refspec main does not match any
error: failed to push some refs to 'origin'

Now this message makes more sense. As mentioned previously, this error tells us we don’t have the “main” branch. There is a couple of ways to resolve this problem. The first one is to rename our currentmaster branch to “main”:

$ git branch -M main

After the rename operation, we can repeat the push command, which will work without problems. At the same time, we can change the name of the branch we want to push frommain to “master” or any name we use as a default in our local repository. The following command will create a “master” branch on the remote repository:

$ git push -u origin master

And lastly, if we want to stick to the “master” name in our local repository and the “main” in our remote repository, we can explicitly set the upstream branch with the following command:

$ git push -u origin master:main

The flag -u will also set the upstream connection between the localmaster branch and the remote “main branch. It means that the next time we can use the command without explicitly identifying the upstream branch:

$ git push

5. Pushing an Empty Repository

Another cause for this problem is pushing an empty repository. However, the reason behind it will be the same – trying to push a branch that doesn’t exist. Let’s assume that we’ve created a new repository. The branches are appropriately named. We added a file but didn’t commit it:

$ echo "# another-test-repo" >> README.md
$ git init
$ git add README.md
$ git branch -M main
$ git remote add origin https://github.com/profile/repository.git
$ git push -u origin main

Although we’re on themain branch, technically, it doesn’t exist. For a branch to be created under .git/refs/heads, it should contain at least one commit. Let’s ensure that the folder .git/refs/heads in our repo is empty at this point:

$ ls .git/refs/heads 

This command should show us an empty folder. Thus, as in the previous example, we’re trying to push a branch that doesn’t exist. A single commit will fix the problem. It will create a branch and make it possible to push the changes:

$ git commit -m "first commit"
$ git push -u origin master

6. Conclusion

Creating and initializing a new local repository isn’t a challenging task. However, skipping steps or blindly following instructions may result in errors. These errors sometimes are not explicitly understandable, especially for new Git users.

In this article, we’ve learned how to deal with refspec errors and what is the reason behind them.

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

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

fix git error:src refspec origin does not match any

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

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.

Понравилась статья? Поделить с друзьями:
  • Squid изменить сообщение об ошибке
  • Square enix код ошибки i2501
  • Squad ошибка при запуске приложения 0xc000007b
  • Squad ошибка unreal engine 4 crash reporter
  • Squad out of video memory ошибка