我使用以下内容克隆存储库:

git clone ssh://xxxxx/xx.git 

但在我更改一些文件并添加和提交它们之后,我想将它们推送到服务器:

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

但我得到的错误是:

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

当前回答

缺少或正在跳过git add。或gitcommit可能会导致此错误:

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'

要修复此问题,请重新初始化并遵循正确的顺序:

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

其他回答

对我来说,只需签出我希望代码推送的分支,然后简单地推送您的代码。

git checkout -b branchname-on-which-i-will-push-my-code

git add .
git commit -m "my commit message"
git push origin branchname-on-which-i-will-push-my-code

万一您在执行gitinit并推送初始提交后仍面临此问题。然后,您可以尝试以下操作:,

git checkout -b "new branch name"
git push origin "new branch name"

您的代码将作为新分支推送。

我遇到了这个错误,因为我使用了字符“&”,所以分支的名称有问题。我只是跳过了“^&”,它起了作用。

我为一个GitHub存储库做了贡献,所以我分叉了这个项目,克隆了它,创建了自己的分支,进行了一些提交,并尝试推送。

此时,我发现我克隆的不是我的fork,而是原始的项目存储库(我没有权限将其推送到)。

所以我更改了.git/config以将原点指向我的存储库。

此时,当我尝试推送时,我收到了错误消息:src-refspec my_awesome_branch不匹配。

我所要做的就是触摸任何文件并提交它(就像你在这个答案中看到的一样):

git touch README
git commit -m "fixing error in my git repo"

然后:

git checkout master
git pull origin master
git push origin master # This will tell my remote repository about my new branch
git checkout my_awesome_branch
git push origin my_awesome_branch # Now it will work

我想是因为你推了一个无效的分支。

通常,因为存储库没有公共的主分支(可能是开发分支)。您可以使用

git branch

查看分支。