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

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 checkout -b master

其他回答

当您添加了文件,忘记提交和推送时,就会发生这种情况。所以提交文件,然后推送。

我在错误的目录中创建了文件,尝试执行git push-u origin master,但我得到了错误。

一旦我cd到当前目录,执行gitpush-uorigin-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

问题是您没有将Git配置为始终在远程和本地创建新分支。

如果您总是想在远程设备上创建新分支以镜像和跟踪本地分支,则永久性的修复方法是:

git config --global push.default current

现在您可以在Git推送时不再出错!

我也面临同样的问题。在我的案例中,我在添加之前犯了错误。按顺序执行这些步骤后,我得到了它。

此外,请确保提供正确的分支名称。

git init
git add .
git commit -m <Your commit message>
git remote add origin "your repository link here"
git push -u origin master