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

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'

当前回答

如果在分离HEAD模式下工作时出现此错误,可以执行以下操作:

git push origin HEAD:remote-branch-name

另请参见:从分离的头部推动Git

如果您所在的本地分支与远程分支不同,可以执行以下操作:

git push origin local-branch-name:remote-branch-name

其他回答

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

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

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

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

截至2022年。

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

cat > README.md

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

git add .
git commit -m "Initial commit"
git push 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

Github将默认分支名称从master更改为main。因此,如果您最近创建了回购,请尝试推送主分支

git push origin main

Github文章

我在Android Studio中处理VCS时遇到了这个确切的问题。事实证明,我所要做的就是:

从“app”文件夹中选择所有文件;转到VCS(顶部选项);“添加”文件;通过终端或通过下拉菜单单击再次提交;推

尤里卡!:D