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

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 3月,Git 2.31:

空GitHub存储库的git克隆将创建一个本地存储库,其中“main”作为默认分支,即使在master上设置了init.defaultBranch!

GitHub支持的Git transfert协议v2将向客户端传递Git clone创建的本地Git存储库的远程默认分支(现在是GitHub的主分支)的名称。

这意味着添加和提交新的提交将在“main”分支上完成,即使Git客户端仍然将master作为默认分支。

下一次推送时,不再出现“src-refspec-master不匹配任何”。

其他回答

只需添加初始提交。遵循以下步骤:

git添加。gitcommit-m“初始提交”git推送原始主机

这对我有用。

我确实遇到了同样的问题,但在我的情况下,创建新存储库时,从一开始就按照页面上给出的步骤进行操作。

只是在这里粘贴:

  echo "# YYYY" >> README.md
  git init
  git add README.md
  git commit -m "first commit"
  git remote add origin https://github.com/XXXX/YYYY.git
  git push -u origin master

在GitBash中键入上述内容。XXXX是用户名,YYYY是存储库名称。

我也遇到了同样的问题,并且发现我没有提交任何文件,所以当我再次尝试提交时,我收到了以下错误消息:

*** Please tell me who you are.

Run

 git config --global user.email "you@example.com"
 git config --global user.name "Your Name"

 to set your account's default identity.
 Omit --global to set the identity only in this repository.

 fatal: unable to auto-detect email address (got 'USER@WINDOWS-HE6I2CL.(none)')

然后我所做的就是在全球范围内添加我的电子邮件和姓名,然后再次提交:

git commit -m 'Initial commit'

然后按下

git push -u origin master

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

我的工作流程:

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

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

添加空目录时遇到此问题。Git不允许推送空目录。这里有一个简单的解决方案。

在要推送到远程的目录中创建文件.gitkeep,并从命令行提交“空”目录:

touch your-directory/.gitkeep
git add your-directory/.gitkeep
git commit -m "Add empty directory"