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

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不允许推送空目录。这里有一个简单的解决方案。

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

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

其他回答

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不匹配任何”。

2020年:

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

我想这可能会对某人有所帮助。我今天做出了我的第一次重大承诺。我试了很多指南,但总是出错。

首先:将文件cd到目录中初始化git:gitinit提交文件:gitcommit要在有任何模糊的bash命令时强制提交,请单击I插入提交消息要继续:单击Esc

要发送到GitHub存储库,请首先确保已添加用户名和电子邮件:

git config --global user.name 'Your name'
git config --global user.email 'name@mail.com'

继续:

git remote add origin https://github repo url

要推送到存储库:

git push -u origin 'https://github.repo url'

它将提交加载到存储库中。

完成!!!

对我来说,只需签出我希望代码推送的分支,然后简单地推送您的代码。

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

我也面临同样的问题。但我注意到,发生此错误时,我的目录是空的。当我创建了一个示例文件并再次推送时,它起了作用。因此,在推送之前,请确保您的文件夹不是空的!!