Gitlab error src refspec master does not match any

I have tried to follow the solutions suggested in this post but it didnt work and I am still getting: src refspec master does not match any. Here is what I did: Followed this solution // adding...

I have tried to follow the solutions suggested in this post but it didnt work and I am still getting: src refspec master does not match any.

Here is what I did:
Followed this solution

// adding the file I created
$ git add .
$ git commit -m 'initial commit'
$ git push origin master
error: src refspec master does not match any.

When doing:

$ git push origin HEAD:master
b40ffdf..a0d1423  HEAD -> master // looks promising

// adding a remote
$ git remote add devstage -f <another git>
$ git merge devstage/master -s recursive -X ours
$ git push -u devstage master
error: src refspec master does not match any.

More information:

$ git branch 
* origin

$ git show-ref
refs/heads/origin
refs/remotes/devstage/master
refs/remotes/origin/HEAD
refs/remotes/origin/devstage
refs/remotes/origin/master
refs/remotes/origin/origin

So I am definitely missing refs/heads/master but dont know how to create it.

Thanks

Community's user avatar

asked Jan 21, 2014 at 17:13

special0ne's user avatar

special0nespecial0ne

5,98317 gold badges66 silver badges105 bronze badges

4

This should help you

git init
git add .
git commit -m 'Initial Commit'
git push -u origin master

answered Nov 26, 2015 at 5:42

nithinreddy's user avatar

3

From git branch it appears that somehow your local branch name is «origin».

You can rename the branch with -mv flag, like this:

git branch -mv origin master

After this git branch should show master :-)

Just to make sure the name is indeed the only thing that went astray, you can run git log and look at the last few commits — and compare them to the last few commits on bitbucket website.

answered Jan 21, 2014 at 19:29

apprenticeDev's user avatar

apprenticeDevapprenticeDev

8,0293 gold badges22 silver badges25 bronze badges

5

Try to do :

git push origin HEAD:master

answered Nov 29, 2017 at 14:53

Adrien Parrochia's user avatar

1

i have same problem, to solve it, follow these steps

 git init
 git add .
 git commit -m 'message'
 git push -u origin master    

after this, if you still having that error, follow these steps again

 git add .
 git commit -m 'message'
 git push -u origin master 

that worked for me and Hope it will help anyone

answered Mar 4, 2017 at 16:51

ankitkhandelwal185's user avatar

2

Try following command:

git push origin HEAD:master

Git threw the below error when I tried simply git push. So clearly this is because Git matches the local and remote branch while pushing commits. This is the push.default behavior, you can find out more details here.

fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:<Branch_Name>

To push to the branch of the same name on the remote, use

    git push origin <Branch_Name>

To choose either option permanently, see push.default in 'git help config'.

answered Apr 10, 2018 at 7:03

Saikat's user avatar

SaikatSaikat

13k18 gold badges102 silver badges119 bronze badges

0

By just adding an empty commit will fix issue by using

$ git commit -m "empty commit" --allow-empty
$ git push

above. make empty commit without edit then push

answered Jan 12, 2019 at 10:40

Bourbia Brahim's user avatar

Bourbia BrahimBourbia Brahim

14.2k4 gold badges39 silver badges51 bronze badges

0

I was having the SAME ERROR AGAIN AND AGAIN.

I added files in local repository and Trying the command

«git push origin master»

Showed Same Error

ALL I WAS MISSING I DID NOT COMMIT .

» git commit -m ‘message’ «

After Runnig this it worked

answered Mar 2, 2018 at 17:28

arslan ahmed mir's user avatar

2

The clue is in the error

error: src refspec master does not match any.

Github’s recently changed its default branch to main.
Take a look here

On your local setup you could rename your local branch as shown below

git branch -m master main

or you could push from your master to main

git push origin master:main

answered Oct 23, 2021 at 20:17

pcodex's user avatar

pcodexpcodex

1,75215 silver badges16 bronze badges

Run the command git show-ref, the result refs/heads/YOURBRANCHNAME
If your branch is not there, then you need to switch the branch by

git checkout -b "YOURBRANCHNAME"

git show-ref, will now show your branch reference.

Now you can do the operations on your branch.

answered Jul 6, 2017 at 7:08

Sonu's user avatar

SonuSonu

6928 silver badges7 bronze badges

0

In my case the error was caused because I was typing

git push origin master

while I was on the develop branch
try:

git push origin branchname

Hope this helps somebody

answered Jun 17, 2017 at 10:47

aneesh joshi's user avatar

The error demo:

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git add --all

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git status
On branch dev
Initial commit
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   index.html
    new file:   photo.jpg
    new file:   style.css

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git push origin dev
error: src refspec dev does not match any.
error: failed to push some refs to 'git@github.com:yourRepo.git'

You maybe not to do $ git commit -m "discription".

Solution:

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git commit -m "discription"
[dev (root-commit) 0950617] discription
 3 files changed, 148 insertions(+)
 create mode 100644 index.html
 create mode 100644 photo.jpg
 create mode 100644 style.css

007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git push origin dev
To git@github.com:Tom007Cheung/Rookie-s-Resume.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:yourRepo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

answered Dec 17, 2017 at 15:36

Mai's user avatar

MaiMai

1253 silver badges15 bronze badges

1

This is happend to me once I forgot to add files. So I got the same error. All you need to do is add your files.

  1. Add your files => git add . or the name of the files you want to add. you supposed to init first your repo with git init.
  2. Commit your changes => git commit -m 'Initial Commit'.
  3. Now push your changes => git push -u origin master

answered Feb 3, 2020 at 18:28

DINA TAKLIT's user avatar

DINA TAKLITDINA TAKLIT

5,9129 gold badges62 silver badges72 bronze badges

this error occurs when you clone a repo from one branch and you trying to push changes to another branch just try to make sure that you are in the same branch compared to the branch that you are trying to push if it isnot the same just clone your repo again from that specific branch by using git clone -b <branchname> <remote-repo-url> then retry to push changes

answered Sep 12, 2021 at 12:38

Alijon Jumanazarov's user avatar

Ensure that if you are pushing the master branch then ensure that you’re currently in the master branch if not checkout to the master branch and now push your commits. To list all current branches in your working directory use :

git branch 

Your currently working branch should have an asterisk at the beginning for instance if am working on my a devstage branch it would appears as shown below :

*devstage
master

In this case, push the commits in the devstage branch first then perform a git pull request if you want to merge the two branches that is the master and the devstage.

answered May 31, 2021 at 20:02

stanley mbote's user avatar

stanley mbotestanley mbote

8411 gold badge7 silver badges17 bronze badges

Check that you call the git commands from the desired directory (where the files are placed).

answered Mar 19, 2017 at 12:42

NoamG's user avatar

NoamGNoamG

1379 bronze badges

This error can typically occur when you have a typo in the branch name.

For example you’re on the branch adminstration and you want to invoke:
git push origin administration.

Notice that you’re on the branch without second i letter: admin(i)stration, that’s why git prevents you from pushing to a different branch!

answered Oct 6, 2017 at 8:42

Tomasz Wójcik's user avatar

Setup username and password in the git config

In terminal, type

vi .git/config

edit url with

url = https://username:password@github.com/username/repo.git

type :wq to save

answered Oct 14, 2017 at 13:55

Prashanth Sams's user avatar

Prashanth SamsPrashanth Sams

18.3k20 gold badges100 silver badges125 bronze badges

Only because your local branch does not math the one in your remote repository.
git push origin HEAD:master
Enable you to ignore the conflict and upload your commit anyway.

answered Jan 19, 2018 at 1:44

YoungJeXu's user avatar

0

I had the same problem recently. but now resolved this issue. Because, Now GitHub changed master to main. It works well for me. Use git push origin main instead of git push origin master. Hopefully, It will work.

answered Sep 21, 2021 at 15:15

Vintage Coder's user avatar

Vintage CoderVintage Coder

4211 gold badge6 silver badges9 bronze badges

For me, the fix appears to be «git .» (stages all current files). Apparently this is required after a git init?
I followed it by «get reset» (unstages all files) and proceeded with the exact same commands to stage only a few files, which then pushed successfully.

   git . 
   git reset

answered Jun 17, 2017 at 17:01

JohnP2's user avatar

JohnP2JohnP2

1,83118 silver badges17 bronze badges

It happened to me and I discovered that github was trying to verify my account. So you need these 2 commands:

git config --global user.email <your github email>
git config --global user.name <your github username>

answered Nov 30, 2018 at 15:00

jess's user avatar

jessjess

237 bronze badges

FWIW, ran into same error, but believe it came about due to the following sequence of events:

  • Remote Git repo was created with master branch.
  • Local clone was then created.
  • Remote Git repo was then modified to include a dev branch, which was defined as the default branch, in conjunction with permissions added to the master branch preventing changes without a pull request.
  • Code updates occurred in the local clone, ready to be pushed to the remote repo.

Then, when attempting to push changes from the local to the remote, received error «src refspec master does not match any», or when attempting to push to dev, «src refspec dev does not match any».

Because changes were pending in the local clone, I did not want to blast it and refresh.
So, fixed the issue by renaming the local branch to dev

$ git branch -m dev

…followed by the normal push of git push origin dev, which worked this time without throwing the aforementioned error.

answered Sep 4, 2019 at 18:37

Trentium's user avatar

TrentiumTrentium

3,2902 gold badges10 silver badges19 bronze badges

This error is also caused due to an unmatched local branch name.
Make sure that you are giving correct local branch name (check spelling and case sensitivity)
I had the same error because my local branch name was «validated» and was trying to push the changes using git push -f origin validate, updated that to git push -f origin validated worked.

Hope this helps.

answered May 2, 2020 at 15:43

Rupesh's user avatar

RupeshRupesh

7921 gold badge10 silver badges26 bronze badges

I also faced the same error.
In my case below is the scenario.

I have master branch which set as origin.

Other side I have release branch «Release_branch».

I have to fork my feature branch(i.efeature/testBranch) from Release branch.

Below are the steps I did.

$ git checkout Release_branch
$ git pull
$ git checkout feature/testBranch
$ git commit -m "SOME_MESSAGE"
$ git push -u origin feature/testBranch

answered May 4, 2021 at 11:16

rahulnikhare's user avatar

rahulnikharerahulnikhare

1,3081 gold badge16 silver badges25 bronze badges

I had this error (error: src refspec master does not match any) with a new repository, when trying git push origin master, because GitHub changed the default name of the master branch to main.

So, git push origin main is working for me.

answered May 15, 2021 at 10:46

Leon Gilyadov's user avatar

For a new repository, the method works for me:

  1. Remote the files related with git
    rm -rf .git

  2. Do the commit again
    git add . && git commit -m "your commit"

  3. Add the git URL and try to push again
    git remote add origin <your git URL>

  4. And then try to push again

    git push -u origin master -f

  5. Success!

Since it’s a new repository, so it doesn’t matter for me to remove the git and add it again.

answered Nov 24, 2018 at 22:41

backslash112's user avatar

backslash112backslash112

2,0451 gold badge22 silver badges29 bronze badges

I had already committed the changes and added all the files, had the same origin as remote, still kept getting that error. My simple solution to this was just:

git push

answered Sep 1, 2020 at 20:54

Vivek Singh's user avatar

Vivek SinghVivek Singh

1981 silver badge14 bronze badges

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. 

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.

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.

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!

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

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.

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

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Get adcomputer сервер вернул следующую ошибку недопустимый контекст перечисления
  • Get 500 ошибка
  • Get 500 internal server error ajax
  • Get 404 ошибка
  • Get 304 error

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии