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

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'

当前回答

为了避免在2021及以后出现此错误,请在使用git init之前使用此命令

git-config--全局init.defaultBranch main这告诉您的git始终使用main作为默认分支名称,而不是master

其他回答

2020年:

如果30个以上的答案都无效,您可能需要运行git push origin main(在编写此答案时,master已重命名为main)。

首先,确保您使用的是master分支。在我的案例中,分支是主分支而不是主分支。所以我做的是

git push origin main

你可以在这张照片中看到结果

在我的例子中,错误发生在我将更改推送到仅在本地拥有的分支时。远程分支不存在同名。我使用以下命令解决了该错误:

git push --set-upstream origin "your-branch-name"

当您试图将空回购推送到git服务器时,会发生此错误。这可以通过初始化README.md文件来缓解:

cat > README.md

然后键入一些内容,然后输入一个回车键,然后按CTRL+D保存。然后是通常的提交步骤:

git add .
git commit -m "Initial commit"
git push origin master

我遇到了同样的困难。。解决方案是将代码推送给回购,就好像它是一个现有项目,而不是一个正在初始化的全新项目。

git remote add origin https://github.com/Name/reponame.git
git branch -M main
git push -u origin main