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

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 branch
* version-x  # you are in this branch
  version-y

$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'origin_address'

其他回答

对我来说,这似乎是因为我已经完成了提交过程,所以我在尝试再次提交后得到了这个结果。

Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

因为我看到路径是origin/main,所以我将其推到main而不是master,并且它起了作用。

注意:检查以确定主分支是主分支还是主分支。

截至2022年。

2020年:

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

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

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

git config --global push.default current

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

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

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

简短回答:此错误意味着您要在远程推送的分支不存在!

在我的案例中,从2020年10月开始,此后创建的回购协议拥有主分支,而不是之前的主分支。所以我要做的就是:

git push -u origin main 

如果设置了上游,则可以跳过-u标志(就像您已经克隆了它一样)

答对 了这对我有用!希望这有帮助!编码愉快!