Git ошибка error src refspec master does not match any

I clone my repository with: git clone ssh://xxxxx/xx.git But after I change some files and add and commit them, I want to push them to the server: git add xxx.php git commit -m "TEST" git push ...

I clone my repository with:

git clone ssh://xxxxx/xx.git 

But after I change some files and add and commit them, I want to push them to the server:

git add xxx.php
git commit -m "TEST"
git push origin master

But the error I get back is:

error: src refspec master does not match any.  
error: failed to push some refs to 'ssh://xxxxx.com/project.git'

aloisdg's user avatar

aloisdg

21.4k6 gold badges85 silver badges100 bronze badges

asked Nov 15, 2010 at 6:09

sinoohe's user avatar

19

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 main

Success!

Coder's user avatar

Coder

1,1471 gold badge12 silver badges30 bronze badges

answered Sep 27, 2011 at 16:07

baisong's user avatar

baisongbaisong

55.3k1 gold badge14 silver badges7 bronze badges

31

  1. Try git show-ref to see what refs you have. Is there a refs/heads/master?

Due to the recent «Replacing master with main in GitHub» action, you may notice that there is a refs/heads/main. As a result, the following command may change from git push origin HEAD:master to git push origin HEAD:main

  1. You can try git push origin HEAD:master as a more local-reference-independent solution. This explicitly states that you want to push the local ref HEAD to the remote ref master (see the git-push refspec documentation).

Philharmonic HE's user avatar

answered Nov 15, 2010 at 11:24

Vi.'s user avatar

Vi.Vi.

36.1k18 gold badges95 silver badges146 bronze badges

22

I also had a similar error after deleting all files on my local computer, and I have to clean up all files in the repository.

My error message was something like this:

error: src refspec master does not match any.
error: failed to push some refs to 'git@github ... .git'

And it was solved by executing the following commands:

touch README
git add README

git add (all other files)
git commit -m 'reinitialized files'
git push origin master --force  # <- caution, --force can delete others work.

Peter Mortensen's user avatar

answered Jan 4, 2012 at 17:03

Aryo's user avatar

AryoAryo

4,2692 gold badges28 silver badges24 bronze badges

4

git push -u origin master
error: src refspec master does not match any.

For that you need to enter the commit message as follows and then push the code:

git commit -m "initial commit"

git push origin master

Successfully pushed to master.

Peter Mortensen's user avatar

answered Aug 9, 2017 at 9:22

VIKAS KOHLI's user avatar

VIKAS KOHLIVIKAS KOHLI

7,8763 gold badges49 silver badges57 bronze badges

2

For me I had to make sure the public key is properly configured on the server (appended in ~/.ssh/authorized_keys) and in GitHub/Bitbucket (added to my SSH keys on GitHub or Bitbucket) — they need to match. Then:

git add --all :/
git commit -am 'message'
git push -u origin master

grg's user avatar

grg

4,5803 gold badges33 silver badges48 bronze badges

answered Sep 2, 2014 at 1:56

pyfork's user avatar

pyforkpyfork

3,5972 gold badges20 silver badges18 bronze badges

0

I found this happened in a brand new repository after I Git added only a directory.

As soon as I added a file (e.g. a README), Git push worked great.

Peter Mortensen's user avatar

answered Sep 25, 2011 at 1:44

Andrew E's user avatar

Andrew EAndrew E

7,3693 gold badges39 silver badges38 bronze badges

7

Missing or skipping git add . or git commit may cause this error:

git push -u origin master
Username for 'https://github.com': yourusername
Password for 'https://yourusername@github.com': 
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/yourusername/foobar.git'

To fix it, reinitialize and follow the proper sequence:

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

Eric Leschinski's user avatar

answered Nov 3, 2012 at 20:30

aug2uag's user avatar

aug2uagaug2uag

3,3293 gold badges30 silver badges52 bronze badges

4

To fix it, re-initialize and follow the proper code sequence:

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

Werner's user avatar

Werner

2,5171 gold badge26 silver badges38 bronze badges

answered Jan 12, 2015 at 17:30

pratik kumar's user avatar

0

This happens too when you are in a specific branch and try to push another branch that does not exist yet, like:

$ git branch
* version-x  # you are in this branch
  version-y

$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'origin_address'

tanius's user avatar

tanius

12.2k3 gold badges44 silver badges57 bronze badges

answered May 23, 2012 at 17:43

wilsonfoz's user avatar

wilsonfozwilsonfoz

8478 silver badges9 bronze badges

7

I faced the same problem, and I used --allow-empty:

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

Supplement

One of main reasons of this problem is that some Git servers, such as BitBucket, don’t have their master branch initialized when a fresh repository is cloned.

answered Oct 25, 2018 at 1:33

Jin Kwon's user avatar

Jin KwonJin Kwon

19.4k12 gold badges107 silver badges170 bronze badges

1

Make sure you’ve added first, and then commit/ push:

Like:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master

answered Apr 28, 2018 at 7:12

Saurabh Singh's user avatar

Problem faced

I had the same problem when I was creating a new repository on GitHub and linking it with my react-app in the client computer I have.

I used the following steps:

Commands used before the problem

git init
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

My mistake

But as you can see my mistake was not using the git add . command
I did this mistake because I already had README.md file and GitHub instructs us with basic commands while creating the repository.

My solution

My solution is to use git add . after git init command.

Use the following set of commands in the same order to overcome the problem:

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

answered Jun 14, 2021 at 7:28

Aswin Barath's user avatar

3

I faced the same issue some days ago.

If you created a new repository nowadays (2020) then the default branch is main on GitHub.

You can check on GitHub now in your repository branches.

And you can also check the branch in the terminal by running the command:

git branch

So that’s why you need to run

git push origin main

instead of

git push origin master

Peter Mortensen's user avatar

answered Dec 16, 2020 at 18:47

Arslan Ahmad khan's user avatar

1

Two possibilities :

1- Either you forgot to include the .gitignore file.

Here are all the steps required:

  1. Create an empty Git repository on remote,

  2. On local create the .gitignore
    file for your project. GitHub gives you a list of examples here

  3. Launch a terminal, and in your project do the following commands:

    git remote add origin YOUR/ORIGIN.git
    
    git add .
    
    git commit -m "initial commit or whatever message for first commit"
    
    git push -u origin master
    

2- Or you are trying to create a new Github project.

Github replaced master with main as the default branch name. To resolve the issue :

  1. On your local project:
    1. remove the .git folder if it exists
    2. recreate a clean repository by launching the following in your project:

in the terminal:

git init

git add .

git commit -m "YOUR FIRST MESSAGE HERE"

git branch -M main

git remote add origin _GIT_LINK_TO_PROJECT_HERE_

git push -u origin main

2

For me,following worked to move untracked files:

git add --all

Next, I followed similar steps

 git commit -m "First commit"

Then,

git remote add origin git@github.....

Last but not the least:

git push -u origin master

As you do this, Windows security will pop up asking for your username and password.

bleistift2's user avatar

answered Jan 13, 2020 at 6:01

Areeha's user avatar

AreehaAreeha

8237 silver badges11 bronze badges

1

You probably forgot the command git add . after the git init command.

Lucas's user avatar

Lucas

4962 gold badges12 silver badges16 bronze badges

answered Apr 25, 2019 at 14:37

Sumer's user avatar

SumerSumer

2,63124 silver badges24 bronze badges

0

After the GitHub update 01.10.20 you should use main instead of master.

Do it like these way…

  1. Create a repository on GitHub
  2. Delete existing .git file on your local directory
  3. Go to local project directory and type git init
  4. git add .
  5. git commit -m"My First Commmit"
  6. Now check your branch name it will be master in your local project
  7. git remote add origin <remote repository URL past here from the github repository> then type git remote -v
  8. git push -f origin master
  9. Now check the github repository you will see two branch 1. main 2. master
  10. In your local repository create new branch and the branch name will be main
  11. git checkout main
  12. git merge master
  13. git pull origin main
  14. git push -f origin main

Note: from 01.10.20 github decided use main instead of master branch use default branch name

answered Oct 9, 2020 at 16:17

iamtheasad's user avatar

iamtheasadiamtheasad

88710 silver badges13 bronze badges

1

Just add an initial commit. Follow these steps:

  • git add .

  • git commit -m "initial commit"

  • git push origin master

This worked for me.

Peter Mortensen's user avatar

answered Dec 28, 2017 at 7:02

NeeruKSingh's user avatar

NeeruKSinghNeeruKSingh

1,5273 gold badges21 silver badges26 bronze badges

My issue was that the ‘master’ branch hadn’t been created locally yet.

A quick

git checkout -b "master"

created the master branch, at which point, a quick

git push -u origin master

pushed the work up to the Git repository.

answered Dec 12, 2014 at 19:38

Anthony's user avatar

AnthonyAnthony

12.9k13 gold badges57 silver badges78 bronze badges

0

Maybe the branch is main instead of master.

Try

git push origin HEAD:main

or

git push origin main

Peter Mortensen's user avatar

answered May 20, 2021 at 18:21

Sankalp Gour's user avatar

1

I have faced the same issue, and this solved my problem:

Just make a branch:

git checkout -b "master"

After that,

git push -u origin master

Boom.

Peter Mortensen's user avatar

answered Jun 2, 2021 at 11:01

Alamin's user avatar

AlaminAlamin

1,71911 silver badges29 bronze badges

2

Feb, 2022 Update:

If your branch is «main»:

enter image description here

Run this command:

git push origin main

If your branch is «master»:

enter image description here

Run this command:

git push origin master

Henry Ecker's user avatar

Henry Ecker

33.5k18 gold badges35 silver badges54 bronze badges

answered Feb 28, 2021 at 12:20

Kai - Kazuya Ito's user avatar

1

This happens when you have added your file, forgot to commit and pushing.
So commit the files and then push.

answered Dec 3, 2011 at 13:29

user993563's user avatar

user993563user993563

18k10 gold badges42 silver badges55 bronze badges

0

  1. First, git add .
  2. Second, git commit -m "message"
  3. Third, git push origin branch

Please check for spelling mistakes because that could also give that error.

Peter Mortensen's user avatar

answered Jun 11, 2015 at 14:15

Alwan Mortada's user avatar

If you get this error while working in detached HEAD mode, you can do this:

git push origin HEAD:remote-branch-name

See also: Making a Git push from a detached head

If you are on a different local branch than the remote branch, you can do this:

git push origin local-branch-name:remote-branch-name

Peter Mortensen's user avatar

answered Mar 2, 2018 at 14:36

snap's user avatar

snapsnap

1,4611 gold badge16 silver badges20 bronze badges

I also followed GitHub’s directions as follows below, but I still faced this same error as mentioned by the OP:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master

For me, and I hope this helps some, I was pushing a large file (1.58 GB on disk) on my MacOS. While copy pasting the suggested line of codes above, I was not waiting for my processor to actually finish the add . process. So When I typed git commit -m "message" it basically did not reference any files and has not completed whatever it needs to do to successfully commit my code to GitHub.

The proof of this is when I typed git status usually I get green fonts for the files added. But everything was red. As if it was not added at all.

So I redid the steps. I typed git add . and waited for the files to finish being added. Then I followed through the next steps.

Peter Mortensen's user avatar

answered Mar 22, 2019 at 16:16

Gel's user avatar

GelGel

2,7562 gold badges17 silver badges25 bronze badges

It happens if you forget to commit before pushing for the first time. Just run:

git commit -m "first commit"

Peter Mortensen's user avatar

answered Sep 8, 2019 at 9:56

Badr Bellaj's user avatar

Badr BellajBadr Bellaj

10.6k2 gold badges40 silver badges40 bronze badges

To check the current status, git status.

And follow these steps as well:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master

Peter Mortensen's user avatar

answered Sep 12, 2019 at 7:48

Dinith's user avatar

DinithDinith

73912 silver badges21 bronze badges

This just mean you forgot to do the initial commit, try

git add .
git commit -m 'initial commit'
git push origin master

answered May 4, 2014 at 14:18

xuri's user avatar

xurixuri

8006 silver badges18 bronze badges

1

I had the same problem when I missed to run:

git add .

(You must have at least one file, or you will get the error again.)

Peter Mortensen's user avatar

answered Feb 11, 2017 at 21:49

neoDev's user avatar

neoDevneoDev

2,7873 gold badges31 silver badges64 bronze badges

0

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.

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

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!

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

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

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

  • Git как изменить сообщение последнего коммита
  • Git как изменить почту
  • Git как изменить название коммита после push
  • Git как изменить коммит на удаленном репозитории
  • Git как изменить имя пользователя

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

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