Message 'src refspec master does not match any' when pushing commits in Git

3,366,452

Solution 1

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!

Solution 2

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

Solution 3

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.

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

Solution 5

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
Share:
3,366,452
sinoohe
Author by

sinoohe

Like gnu linux world, photography , programming

Updated on May 01, 2022

Comments

  • sinoohe
    sinoohe almost 2 years

    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'
    
    • tandrewnichols
      tandrewnichols almost 11 years
      @Marco That's not a duplicate. That one is a very specific issue about pushing a local branch to a remote branch. This one is about initializing a repo and pushing it up. They produce the same error, but the REASONS they produce that error and the fixes are entirely different. Also, sinoohe, you should accept an answer. Probably the first one, seeing as it answers the question and has helped over 350 people.
    • IgorGanapolsky
      IgorGanapolsky over 10 years
      Did you set up your git config commands to install and configure git globally on your machine?
    • Samitha Chathuranga
      Samitha Chathuranga almost 9 years
      Hope this post would be useful to somebody- samranga.blogspot.com/2015/07/… The error in the question can be popped even when tried to Create a git BitBucket repository from an already locally existing project
    • Tom Howard
      Tom Howard over 8 years
      I received this error trying to push the wrong branch name. Resolved using git status to get the proper one.
    • Jaime
      Jaime over 8 years
      Adding a comment to call out @aug2uag's alternative answer - sleepily skipping git commit can cause this error, as well!
    • jww
      jww over 6 years
      Yet another simple task made difficult by Git. The Git devs should use Stack Overflow as feedback in their SDLC loop. 850,000+ people should indicate something is seriously wrong with Git's workflow. They need to hire a UX expert because they clearly cannot git it right on their own.
    • Blasanka
      Blasanka almost 6 years
      If you didnt add git add with dot or some files this error also will appear.
    • Persistent Plants
      Persistent Plants almost 6 years
      The above error can come up when you have an incorrect branch name, so for others facing the same issue it would be helpful to double check that.
    • lesolorzanov
      lesolorzanov over 5 years
      Here note.yuhc.me/2015/01/git-push-error-refspec-not-match I found the ebst answer. If you just cloned the repo and it is not empty then you can push by specifying that the branch is in the HEAD like this $ git push origin HEAD:<branch>
    • saber tabatabaee yazdi
      saber tabatabaee yazdi about 4 years
      check your privilege in my case i need to check my permission i have two private git repositories and this second account is admin of that new repo and first one is my default user account and i should grant permission to first
    • M Y
      M Y almost 4 years
      Or you may wrote non-existence branch.
    • Y00P
      Y00P almost 4 years
      FWIW I got this error when I tried to push to an uninitialized remote repo with no local commits done. Doing initial local commit then pushing worked.
    • Harini Sj
      Harini Sj over 3 years
      Recently Github/Git does not have a default "master" branch. "master" has been changed to "main" branch. So this may be a possible reason for this error.
    • SaginiChan
      SaginiChan about 3 years
      will you honestly just be corteous enough to accept an answer?
    • Vikranth Inti
      Vikranth Inti almost 3 years
      @sinoohe, if you have only main branch, try "git push origin HEAD :main " It worked for me.
  • sinoohe
    sinoohe over 13 years
    my master branch wasn't on top of commits ! so i created a branch that it was at the end of all branchs and i pushed them to the server:
  • sinoohe
    sinoohe over 13 years
    git checkout -b testbranch ; git push origin testbranch:master
  • James Lai
    James Lai over 12 years
    git show-ref showed my branch; the git push origin HEAD:<branch> worked for me.
  • Kumar
    Kumar almost 12 years
    Don't just follow this step blindly, look at what @Vi has mentioned, and then modify your push command to correct ref.
  • shkschneider
    shkschneider almost 12 years
    You just saved me Vi. Thank you for git push origin HEAD:master. But why something like git push --force origin master does not work?
  • Lemmings19
    Lemmings19 about 11 years
    The other answers did not solve the problem I was having (for instance, I had already committed and still had this error), but doing a git push origin BRANCH --force worked. Thank you!
  • Ben
    Ben about 11 years
    Damn, happened to me as well. Been using XCode for Git for too long and took it for granted that you need to commit before pushing.
  • penner
    penner almost 11 years
    LOL. I was trying to push to origin master but that branch didn't exist. It was called origin stable.
  • asyncwait
    asyncwait almost 11 years
    @gms8994 -- you're correct. if someone creates a branch as a first thing instead of pushing into master, the same unfriendly error shows up. use git push origin <branch> instead of master in case you attempt to git checkout -b <branch> before any FIRST push after initial git remote add origin <url>
  • Admin
    Admin about 10 years
    This probably works because git doesn't actually track directories, only files. So if a directory is empty, git won't actually add it.
  • Admin
    Admin about 10 years
    Why did you need to use the full URL for your remote instead of just setting up an alias for it with git remote add origin <url>?
  • Admin
    Admin about 10 years
    See this earlier answer. I suspect that you needed to add a file because git won't track empty directories.
  • Mr.Chowdary
    Mr.Chowdary almost 10 years
    @Vi. It worked for me, Thanks a lot.. But everytime i have to use the same command.. else showing the same error when trying to commit by git push origin master .. Please help me to overcome this..
  • Vi.
    Vi. almost 10 years
    @Mr.Chowdary, You can configure the "default push location" of the branch, like in git push --set-upstream origin refs/heads/the_remote_branch. Also you can create a script or alias that will do git push origin HEAD:/refs/heads/master with little typing.
  • Admin
    Admin almost 10 years
    If you want to push your current branch, an easy way to do it is to simply do git push origin HEAD, or git push origin @ if you're using a recent version of Git, or git push origin head if you're using Windows or OS X.
  • sage
    sage almost 10 years
    I suspect a great many of us who came here via web search actually mistyped the name!
  • aug2uag
    aug2uag almost 10 years
    that's correct, the business end is more specifically git remote add __REMOTE_NAME__ __URL_OR_SSH__, and above the remote name is "origin"
  • Bhanu Pratap Singh
    Bhanu Pratap Singh almost 9 years
    The most probable reason for this error is that all the files are untracked and have not been added. git add --all in case you wish to add all the files Or you can selectively add files. Then git commit -m "Initial comment", git push origin master. This will surely work.
  • Michael Durrant
    Michael Durrant almost 9 years
    Fixes different issue which is nice but not really an answer to This actual question.
  • Michael Durrant
    Michael Durrant almost 9 years
    push --force could also completely blew away co-workers hard work. Added warning by it.
  • Michael Durrant
    Michael Durrant almost 9 years
    They added and committed in their question so that was not the issue even though this helps other people.
  • Michael Durrant
    Michael Durrant almost 9 years
    This answer did work however because of the -u option that was used here.
  • Michael Durrant
    Michael Durrant almost 9 years
    The OP added a file (xx.php) so this was not the problem in this case even though in other cases this can be a problem and adding a file a solution of that problem.
  • Michael Durrant
    Michael Durrant almost 9 years
    The -u may have helped here.
  • Samitha Chathuranga
    Samitha Chathuranga almost 9 years
    Hope this post would be useful to somebody- samranga.blogspot.com/2015/07/… The error in the question can be popped even when tried to Create a git BitBucket repository from an already locally existing project
  • James Wierzba
    James Wierzba over 8 years
    Such a simple solution to a frustrating problem. I was testing the creation and clonining of repos and created empty directories not files.
  • Anandaraja_Srinivasan
    Anandaraja_Srinivasan over 8 years
    This solved my problem. I think git add did it. While pushing things at first git doesn't recognize things, may be that's why I had the problem. git add command solved my problem. also after that i was able to push without --force. Thanks Aryo
  • Jen
    Jen over 8 years
    Also happens if you type the name of your branch incorrectly. Typos.
  • swt83
    swt83 over 8 years
    I migrated to a new machine and was getting these errors. Setting my GIT info fixed it.
  • Sunil Sharma
    Sunil Sharma over 8 years
    What if no refs/heads/master found ??
  • Vi.
    Vi. over 8 years
    @SunilSharma, Then use some ref that is found, or explicit hex commit-id.
  • Trip
    Trip about 8 years
    This seems to only work if I force it .. git push origin HEAD:master --force, but I still can't do a normal git push . I'm curious what I need to do to get past this original error.
  • Vi.
    Vi. about 8 years
    @Trip, Note that there is also option --force-with-lease, which is safer that usual --force, especially when you use the repository not alone.
  • dance2die
    dance2die almost 8 years
    git commit -m 'initial commit' should be double quoted. 'git commit -m "initial commit", at least on windows.
  • Yash Agrawal
    Yash Agrawal over 7 years
    please do write git commit -m "initial commit"
  • tanius
    tanius over 7 years
    In the above case, the problem is of course that there's no local branch master, so you can't push it. You either want to push an existing branch – or create the master branch and then push it, like this: git checkout -b master; git push -u origin master;
  • Arno
    Arno about 7 years
    there has to be something to commit, you get no error if you commit an empty directory but then this error occurs when you try to push
  • Sebastian Ärleryd
    Sebastian Ärleryd almost 7 years
    I just got this when I misspelled the branch name.
  • Mona Jalal
    Mona Jalal almost 7 years
    nothing added to commit but untracked files present Monas-MacBook-Pro:02_02 mona$ git push origin master error: src refspec master does not match any. error: failed to push some refs to 'https://github.com/monajalal/jupyter_notebooks.git' didn't work for me!
  • protoEvangelion
    protoEvangelion almost 7 years
    My local branch was spelled "sheduler" and I was doing git push origin scheduler. HA! One letter off will kill you in programming. lol
  • protoEvangelion
    protoEvangelion almost 7 years
    My problem was I was misspelling the name of my local branch when pushing. I was doing git push scheduler when my local branch was named sheduler. Once I fixed the local name to say scheduler it works. HAHA :)
  • cautionbug
    cautionbug almost 7 years
    Thank you @GlenSolsberry - The HEAD: prefix was exactly what i needed for this problem. i have local branches that look like dev/something-or-other and the remote is just dev. Apparently git was confused about the path-like branch names, so git push <remote> HEAD:dev worked perfectly.
  • CarlosAS
    CarlosAS about 6 years
    Another possible reason: you don't actually have a branch called master
  • Ferhat KOÇER
    Ferhat KOÇER about 6 years
    No, I mean if you forget git commit -m "xxxx" command, you get the same problem.Thank you I edited my comment
  • Ashley Duncan
    Ashley Duncan almost 6 years
    Not sure if I missed it in the fine print but an important point is you need to add a file to a new repo before committing.
  • VIKAS KOHLI
    VIKAS KOHLI over 5 years
    I checked it's working. Please ignore -u option and then try
  • c4sh
    c4sh over 5 years
    This happened to me when trying to psh a tag that didn't exist.
  • Chris
    Chris over 5 years
    If this answer results in no results (just blank..) then if you have the same case as me, you may have just forgot to 'add' any files. So I did: 'git add --all', then 'git commit -m "initial commit"', then 'git push origin HEAD:master' to resolve.
  • Rahul Mishra
    Rahul Mishra about 5 years
    I wanted to push changes in new branch instead of master. I just changed the above mentioned code with git push -u origin HEAD:feature/random_name and that worked for me.
  • old-monk
    old-monk about 5 years
    for me it was unnecessary single quote in the branch name 'cmd-line-runnable' .. that I must have put by mistake. git show-ref helped to understand it.
  • mLstudent33
    mLstudent33 about 5 years
    @baisong this does not work if I git remote set-url origin <my_git_repo.git> and try to git add . , git commit -m "intial commit', git push origin master
  • Usman Ali Maan
    Usman Ali Maan about 5 years
    Oops! Never committed! section worked for me , i had even committed but there was some issue that's why it make error for me
  • Andrew Koster
    Andrew Koster almost 5 years
    This is what happens when you follow BitBucket's (incomplete) instructions for setting up your repo. Lol.
  • Greg Wozniak
    Greg Wozniak almost 5 years
    Any chance to avoid this for Heroku? It is annoying when you often create projects to be forced to push some random file, especially that if you set-up build, heroku will reject the push. For every new project my pipeline breaks if I won't pull using cli and push. Tried git push -f heroku master:refs/heads/master and git push -f heroku HEAD:master - both don't work for the first time...
  • Kewal Shah
    Kewal Shah over 4 years
    This also might occur if you have not added any files in the staging area. You can run git status to check which files have you staged.
  • rvictordelta
    rvictordelta over 4 years
    8 years later this saved me some headache!
  • Peter Mortensen
    Peter Mortensen over 4 years
    What is the limit?
  • Peter Mortensen
    Peter Mortensen over 4 years
    Re "skipped": Do you mean "escaped"?
  • Peter Mortensen
    Peter Mortensen over 4 years
    What is "fastlane match nuke development", etc.? Some kind of weird Git client?
  • blackjacx
    blackjacx over 4 years
    Nope, in fact fastlane match nuke development deletes the development certificates and profiles from the git credential store and the Developer Portal (I edited my answer).
  • Kodali444
    Kodali444 over 4 years
    In my case, 1--> git init 2---> git add origin....etc 3---> git git push -u origin master ===>Then I got the above error. ===>Then I executed following 2 commands, it's disappear. ---> git add * ---> git commit -m "Some message" --->git git push -u origin master ===>Worked fine for me, in my case.
  • Andrew E
    Andrew E over 4 years
    @rao yep, adding an origin isn't enough, you must add and commit files. But why do you have "git git push..."? Is the second "git" a typo?
  • ZX9
    ZX9 over 4 years
    The issue here seems to be completely different than the OP's...but it seems many including me had this issue.
  • Kodali444
    Kodali444 over 4 years
    Yes it's a typo. Thank you
  • pedram bashiri
    pedram bashiri over 4 years
    How is this the error in question related to .gitignore?! If it is related (which I highly doubt) you should explain it in your answer. Thanks
  • Abdallah Abdillah
    Abdallah Abdillah over 4 years
    did you mean ` git add --all` with two dashes
  • Taha Farooqui
    Taha Farooqui almost 4 years
    Perfect answer !!
  • M Y
    M Y almost 4 years
    Or you may wrote non-existence branch.
  • Pyae
    Pyae almost 4 years
    Had the same problem and it turn out that I typo branch name.
  • Napster Scofield
    Napster Scofield over 3 years
    I forgot to commit then ran to this. JUST COMMIT
  • aahnik
    aahnik over 3 years
    Even that failed. My local branch was master. I created a main locally and then push worked with -u flag
  • vishwampandya
    vishwampandya over 3 years
    Thanks a lot. It worked. This was really a major recent update by github
  • Vishnu
    Vishnu over 3 years
    I see that a few people are downvoting this answer. Just to be clear, I came across this question when I got the same error when pushing my changes to the master branch. On further reading, I got to know that GitHub has changed master to main and I just wanted people to know about it and that was the reason for this answer.
  • Joseph Brenner
    Joseph Brenner over 3 years
    I think you mean "main" not "live". As of June 2020 they've dropped "master" in favor of "main.
  • Joseph Brenner
    Joseph Brenner over 3 years
    As CarlosAS has pointed out, you can get this error if there's no branch "master", and that's actually a common case right now, because as of June 2020 github has stopped using branches named "master" in favor of "main".
  • Jboy Flaga
    Jboy Flaga over 3 years
    Hi @JosephBrenner, I already removed the reference to Github.
  • Aaditya Ura
    Aaditya Ura over 3 years
    master is changed to main now.
  • zinovyev
    zinovyev over 3 years
    That is valid only for new repositories. So this answer is completely inaccurate. More info here: github.com/github/renaming
  • SaltyCatFish
    SaltyCatFish over 3 years
    I don't think its inaccurate at all. I came here because I received the same error message. The error happened to be that "master" was changed to "main" on the remote side and I never updated the remote URL.
  • Hileamlak Yitayew
    Hileamlak Yitayew over 3 years
    I am getting the opposit , I have seen that my default is main but when I try men I get error what do you think I should do
  • Ayush Jain
    Ayush Jain over 3 years
    As far as I could understand, this is because you haven't created any branch named men. If you want a branch named men you can use git checkout -b men and then you would be able to push this branch to remote.
  • saber tabatabaee yazdi
    saber tabatabaee yazdi over 3 years
    github rename the master with main. so you can use git remote origin main
  • Jacobs2000
    Jacobs2000 over 3 years
    Tried this solution and got refs/heads/master. Then git push origin master worked.
  • Heath
    Heath over 3 years
    Thanks, this was also a recent update for gitlab, just in case anyone was wondering.
  • gridtrak
    gridtrak over 3 years
    git --version 2.29.1.windows.1 switched from "master" to "main". My GOGS server's templates still use "master". After initializing my repository locally, all I need to do is git push -u origin main instead of git push -u origin master
  • ultrasounder
    ultrasounder over 3 years
    The original announcement from GH CEO Nat Friedman on Twitter in response to the #BLM Movement. twitter.com/natfriedman/status/1271253144442253312
  • Kinyugo
    Kinyugo about 3 years
    I ran into this and the problem was I hadn't set my branch git branch -M <branch_name>
  • Mehdi Abbassi
    Mehdi Abbassi about 3 years
    I did commit, but when I try to push my commit to the main branch, which is now the default branch in GitHub, I get this error. If I push to master, there is no error, but then I have two branches in my GitHub!
  • Kale
    Kale about 3 years
    I gave a similar answer that helped me on this question: stackoverflow.com/a/66737426/5996491
  • Rafiq
    Rafiq about 3 years
    giving initial commit solved my problem: git commit -a -m"commit message"
  • zac
    zac about 3 years
    master instead of main solve me problem
  • Karen Goh
    Karen Goh almost 3 years
    I tried your method but still getting error: src refspec master does not match any
  • froggomad
    froggomad almost 3 years
    Glad I wasn't the only one
  • sailfish009
    sailfish009 almost 3 years
    Thanks, this works for me. other solutions didn't.
  • Stefan
    Stefan almost 3 years
    Run git add . then git commit -m "first commit", then push again
  • yasnil
    yasnil almost 3 years
    putting the HEAD: before the remote branch name worked like a magic.
  • iifast
    iifast over 2 years
    I also recommand watching this tutorial : youtu.be/yZdmcMQkQRo
  • Balasubramanian S
    Balasubramanian S over 2 years
    This was my problem. Tried so many things and then realized it was main instead of master.
  • Admin
    Admin over 2 years
    this is the only one that actually worked.
  • Promise Preston
    Promise Preston over 2 years
    Exactly. This was the issue for me. I was trying to push to a dev branch from my local that does not exist yet on my local. I was in the main branch, so I had to do git checkout -b dev and then git push origin dev. Thank you.
  • boardtc
    boardtc over 2 years
    I did not get a Gerrit url to indicate the patch was updated but it was!
  • Vintage Coder
    Vintage Coder over 2 years
    Github now changed git push origin master to git push origin main
  • east1000
    east1000 over 2 years
    This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
  • Arya Mohanan
    Arya Mohanan over 2 years
    I don't know why you guys said like the answer is not valid, recently I had faced the same issue as mentioned the question and resolved by the changing the branch. Just posted here to help others who go through the same problem
  • Po-Yu Kao
    Po-Yu Kao over 2 years
    Thanks for your help. It is really helpful.
  • Tora Tora Tora
    Tora Tora Tora over 2 years
    git branch -M main worked for me.
  • fiorentinoing
    fiorentinoing over 2 years
    An other reason is that you have no complete repo, but only a shallow one. (i.e., you cloned with --depth=1 flag or something similar)
  • Harish
    Harish over 2 years
    I got the same issue and realized that branch was on main. then I followed this and got resolved.
  • iownthegame
    iownthegame over 2 years
    this saves my day!!! I mixed it up with main and master
  • Lucas
    Lucas over 2 years
    What’s git *create remote?
  • mustafa hasria
    mustafa hasria about 2 years
    What about if it doesn't shown anything
  • Johnny Bravo
    Johnny Bravo about 2 years
    Thanks, I was missing the ignore document.
  • Andy
    Andy about 2 years
    same here. Thanks!
  • Sunderam Dubey
    Sunderam Dubey about 2 years
    push is always done on that branch which is selected.
  • Panama Jack
    Panama Jack about 2 years
    This git branch -M main is important which is why it wasn't working for me. It expects master if you don't put anything.