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

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.

$ git push -u origin main после этой комманды выдает
error: src refspec main does not match any
error: failed to push some refs to ‘https://github.com/…’
при этом, если я сделаю $ git push -u origin master, то создается новая ветка и туда отправляются нужные файлы, правда как с ней взаимодействовать — непонятно, все выводится списком
при этом я пытаюсь принять пуш, но ничего не происходит
62dc2baeeea93215697135.png

а еще если попытаться опять что-нибудь закоммитить $ git commit -m «#1»
то получается:
On branch master
nothing to commit, working tree clean
то есть локально у меня главная ветка master? а на гите main, из-за этого не может быть проблем?


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

    23 июл. 2022

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

Команда git push -u origin main делает отправку локальной ветки main во внешний репозиторий origin, но ветки main не существует, о чем вам и сообщили в ошибке.

Вам нужно либо переименовать master в main:
git branch -M main

Либо так и написать, что вы хотите master отправить во внешний main
git push -u origin master:main

Но судя по скрину, у вас репозиторий не пустой. Вы уже создали там ветку с первоначальным коммитом. Поэтому вы не сможете просто так туда сделать push, так как ваши ветки не имеют общей истории. Это РАЗНЫЕ деревья. В таких случаях можно просо пересадить локальную ветку на вершину внешней через rebase. Либо создать ПУСТОЙ репо, как вы и сделали.

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

*** нет цензурных слов)
в моем репозитории на гите лежал файл readme.md
даже если я его пулил на локальный репозиторий, это не помогало
в итоге создал все заново без всяких доп файлов и нормально запушилось
+ добавил git branch -M main


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

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

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

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

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

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

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

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

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.

Понравилась статья? Поделить с друзьями:
  • 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