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

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

其他回答

当我收到src-refspec错误时,上述解决方案都不适用。

我的工作流程:

推送到远程分支(相同的本地分支名称)删除了远程分支改变了一些东西再次推送到相同的远程分支名称(相同的本地分支名称)获取src-refspec错误。

我通过简单地创建一个新分支并再次推送来修复错误。(奇怪的是,我不能简单地重命名分支,这给我带来了致命的后果:分支重命名失败。)

2022年2月更新:

如果您的分支是“main”:

运行以下命令:

git push origin main

如果您的分支是“master”:

运行以下命令:

git push origin master

我也收到了这个问题,但这是因为我在推送之前意外关闭了服务器。这也会导致同样的错误。

关于Aryo的回答:在我的情况下,我必须使用本地Git存储库的完整URL来推送文件。首先,我删除了当前目录中的所有文件,并创建了README。

添加了更多内容。然后我提交了这些文件,最后推送了它们,给了存储库一个正确的URL。这里,您的存储库是服务器上存储库的名称。

rm -rf *

touch README
git add README
touch file1 file2
git add file1 file2

git commit -m "reinitialized files"
git push git@localhost:yourrepository.git master --force

我也有同样的问题。我通过以下步骤做到了这一点:

1. git commit -m 'message'
2. git config --global user.email "your mail"
3. git config --global user.name "name"
4. git commit -m 'message'
5. git push -u origin master