Message 'src refspec master does not match any' when pushing commits in Git
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
- Try
git show-ref
to see what refs you have. Is there arefs/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 fromgit push origin HEAD:master
togit push origin HEAD:main
- 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 refHEAD
to the remote refmaster
(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
Comments
-
sinoohe almost 2 years
I clone my repository with:
git clone ssh://xxxxx/xx.git
But after I change some files and
add
andcommit
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 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 over 10 yearsDid you set up your git config commands to install and configure git globally on your machine?
-
Samitha Chathuranga almost 9 yearsHope 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 over 8 yearsI received this error trying to push the wrong branch name. Resolved using
git status
to get the proper one. -
Jaime over 8 yearsAdding a comment to call out @aug2uag's alternative answer - sleepily skipping
git commit
can cause this error, as well! -
jww over 6 yearsYet 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 almost 6 yearsIf you didnt add
git add
with dot or some files this error also will appear. -
Persistent Plants almost 6 yearsThe 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 over 5 yearsHere 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 about 4 yearscheck 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 almost 4 yearsOr you may wrote non-existence branch.
-
Y00P almost 4 yearsFWIW 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 over 3 yearsRecently 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 about 3 yearswill you honestly just be corteous enough to accept an answer?
-
Vikranth Inti almost 3 years@sinoohe, if you have only main branch, try "git push origin HEAD :main " It worked for me.
-
-
sinoohe over 13 yearsmy 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 over 13 yearsgit checkout -b testbranch ; git push origin testbranch:master
-
James Lai over 12 years
git show-ref
showed my branch; thegit push origin HEAD:<branch>
worked for me. -
Kumar almost 12 yearsDon't just follow this step blindly, look at what @Vi has mentioned, and then modify your push command to correct ref.
-
shkschneider almost 12 yearsYou just saved me Vi. Thank you for
git push origin HEAD:master
. But why something likegit push --force origin master
does not work? -
Lemmings19 about 11 yearsThe 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 about 11 yearsDamn, 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 almost 11 yearsLOL. I was trying to push to origin master but that branch didn't exist. It was called origin stable.
-
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. usegit push origin <branch>
instead ofmaster
in case you attempt togit checkout -b <branch>
before any FIRST push after initialgit remote add origin <url>
-
Admin about 10 yearsThis probably works because git doesn't actually track directories, only files. So if a directory is empty, git won't actually add it.
-
Admin about 10 yearsWhy 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 about 10 yearsSee this earlier answer. I suspect that you needed to add a file because git won't track empty directories.
-
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. 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 dogit push origin HEAD:/refs/heads/master
with little typing. -
Admin almost 10 yearsIf you want to push your current branch, an easy way to do it is to simply do
git push origin HEAD
, orgit push origin @
if you're using a recent version of Git, orgit push origin head
if you're using Windows or OS X. -
sage almost 10 yearsI suspect a great many of us who came here via web search actually mistyped the name!
-
aug2uag almost 10 yearsthat'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 almost 9 yearsThe 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. Thengit commit -m "Initial comment"
,git push origin master
. This will surely work. -
Michael Durrant almost 9 yearsFixes different issue which is nice but not really an answer to This actual question.
-
Michael Durrant almost 9 yearspush --force could also completely blew away co-workers hard work. Added warning by it.
-
Michael Durrant almost 9 yearsThey added and committed in their question so that was not the issue even though this helps other people.
-
Michael Durrant almost 9 yearsThis answer did work however because of the
-u
option that was used here. -
Michael Durrant almost 9 yearsThe 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 almost 9 yearsThe
-u
may have helped here. -
Samitha Chathuranga almost 9 yearsHope 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 over 8 yearsSuch a simple solution to a frustrating problem. I was testing the creation and clonining of repos and created empty directories not files.
-
Anandaraja_Srinivasan over 8 yearsThis 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 over 8 yearsAlso happens if you type the name of your branch incorrectly. Typos.
-
swt83 over 8 yearsI migrated to a new machine and was getting these errors. Setting my GIT info fixed it.
-
Sunil Sharma over 8 yearsWhat if no refs/heads/master found ??
-
Vi. over 8 years@SunilSharma, Then use some ref that is found, or explicit hex commit-id.
-
Trip about 8 yearsThis seems to only work if I force it ..
git push origin HEAD:master --force
, but I still can't do a normalgit push
. I'm curious what I need to do to get past this original error. -
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 almost 8 years
git commit -m 'initial commit'
should be double quoted. 'git commit -m "initial commit"
, at least on windows. -
Yash Agrawal over 7 yearsplease do write git commit -m "initial commit"
-
tanius over 7 yearsIn 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 about 7 yearsthere 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 almost 7 yearsI just got this when I misspelled the branch name.
-
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 almost 7 yearsMy local branch was spelled "sheduler" and I was doing
git push origin scheduler
. HA! One letter off will kill you in programming. lol -
protoEvangelion almost 7 yearsMy 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 almost 7 yearsThank you @GlenSolsberry - The
HEAD:
prefix was exactly what i needed for this problem. i have local branches that look likedev/something-or-other
and the remote is justdev
. Apparently git was confused about the path-like branch names, sogit push <remote> HEAD:dev
worked perfectly. -
CarlosAS about 6 yearsAnother possible reason: you don't actually have a branch called master
-
Ferhat KOÇER about 6 yearsNo, I mean if you forget git commit -m "xxxx" command, you get the same problem.Thank you I edited my comment
-
Ashley Duncan almost 6 yearsNot 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 over 5 yearsI checked it's working. Please ignore -u option and then try
-
c4sh over 5 yearsThis happened to me when trying to psh a tag that didn't exist.
-
Chris over 5 yearsIf 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 about 5 yearsI 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 about 5 yearsfor 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 about 5 years@baisong this does not work if I
git remote set-url origin <my_git_repo.git>
and try togit add .
,git commit -m "intial commit'
,git push origin master
-
Usman Ali Maan about 5 yearsOops! 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 almost 5 yearsThis is what happens when you follow BitBucket's (incomplete) instructions for setting up your repo. Lol.
-
Greg Wozniak almost 5 yearsAny 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
andgit push -f heroku HEAD:master
- both don't work for the first time... -
Kewal Shah over 4 yearsThis 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 over 4 years8 years later this saved me some headache!
-
Peter Mortensen over 4 yearsWhat is the limit?
-
Peter Mortensen over 4 yearsRe "skipped": Do you mean "escaped"?
-
Peter Mortensen over 4 yearsWhat is "fastlane match nuke development", etc.? Some kind of weird Git client?
-
blackjacx over 4 yearsNope, 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 over 4 yearsIn 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 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 over 4 yearsThe issue here seems to be completely different than the OP's...but it seems many including me had this issue.
-
Kodali444 over 4 yearsYes it's a typo. Thank you
-
pedram bashiri over 4 yearsHow 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 over 4 yearsdid you mean ` git add --all` with two dashes
-
Taha Farooqui almost 4 yearsPerfect answer !!
-
M Y almost 4 yearsOr you may wrote non-existence branch.
-
Pyae almost 4 yearsHad the same problem and it turn out that I typo branch name.
-
Napster Scofield over 3 yearsI forgot to commit then ran to this. JUST COMMIT
-
aahnik over 3 yearsEven that failed. My local branch was master. I created a main locally and then push worked with -u flag
-
vishwampandya over 3 yearsThanks a lot. It worked. This was really a major recent update by github
-
Vishnu over 3 yearsI 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 over 3 yearsI think you mean "main" not "live". As of June 2020 they've dropped "master" in favor of "main.
-
Joseph Brenner over 3 yearsAs 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 over 3 yearsHi @JosephBrenner, I already removed the reference to Github.
-
Aaditya Ura over 3 years
master
is changed tomain
now. -
zinovyev over 3 yearsThat is valid only for new repositories. So this answer is completely inaccurate. More info here: github.com/github/renaming
-
SaltyCatFish over 3 yearsI 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 over 3 yearsI 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 over 3 yearsAs far as I could understand, this is because you haven't created any branch named
men
. If you want a branch namedmen
you can usegit checkout -b men
and then you would be able to push this branch to remote. -
saber tabatabaee yazdi over 3 yearsgithub rename the master with main. so you can use git remote origin main
-
Jacobs2000 over 3 yearsTried this solution and got refs/heads/master. Then git push origin master worked.
-
Heath over 3 yearsThanks, this was also a recent update for gitlab, just in case anyone was wondering.
-
gridtrak over 3 yearsgit --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 ofgit push -u origin master
-
ultrasounder over 3 yearsThe original announcement from GH CEO Nat Friedman on Twitter in response to the #BLM Movement. twitter.com/natfriedman/status/1271253144442253312
-
Kinyugo about 3 yearsI ran into this and the problem was I hadn't set my branch
git branch -M <branch_name>
-
Mehdi Abbassi about 3 yearsI 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 about 3 yearsI gave a similar answer that helped me on this question: stackoverflow.com/a/66737426/5996491
-
Rafiq about 3 yearsgiving initial commit solved my problem: git commit -a -m"commit message"
-
zac about 3 years
master
instead ofmain
solve me problem -
Karen Goh almost 3 yearsI tried your method but still getting error: src refspec master does not match any
-
froggomad almost 3 yearsGlad I wasn't the only one
-
sailfish009 almost 3 yearsThanks, this works for me. other solutions didn't.
-
Stefan almost 3 yearsRun
git add .
thengit commit -m "first commit"
, then push again -
yasnil almost 3 yearsputting the HEAD: before the remote branch name worked like a magic.
-
iifast over 2 yearsI also recommand watching this tutorial : youtu.be/yZdmcMQkQRo
-
Balasubramanian S over 2 yearsThis was my problem. Tried so many things and then realized it was main instead of master.
-
Admin over 2 yearsthis is the only one that actually worked.
-
Promise Preston over 2 yearsExactly. 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 themain
branch, so I had to dogit checkout -b dev
and thengit push origin dev
. Thank you. -
boardtc over 2 yearsI did not get a Gerrit url to indicate the patch was updated but it was!
-
Vintage Coder over 2 yearsGithub now changed
git push origin master
togit push origin main
-
east1000 over 2 yearsThis 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 over 2 yearsI 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 over 2 yearsThanks for your help. It is really helpful.
-
Tora Tora Tora over 2 years
git branch -M main
worked for me. -
fiorentinoing over 2 yearsAn 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 over 2 yearsI got the same issue and realized that branch was on main. then I followed this and got resolved.
-
iownthegame over 2 yearsthis saves my day!!! I mixed it up with
main
andmaster
-
Lucas over 2 yearsWhat’s
git *create remote
? -
mustafa hasria about 2 yearsWhat about if it doesn't shown anything
-
Johnny Bravo about 2 yearsThanks, I was missing the ignore document.
-
Andy about 2 yearssame here. Thanks!
-
Sunderam Dubey about 2 years
push
is always done on thatbranch
which is selected. -
Panama Jack about 2 yearsThis
git branch -M main
is important which is why it wasn't working for me. It expectsmaster
if you don't put anything.