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

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 checkout -b "master"

创建了主分支,此时

git push -u origin master

将工作推到Git存储库。

我也有同样的问题。我通过以下步骤做到了这一点:

1. git commit -m 'message'
2. git config --global user.email "your mail"
3. git config --global user.name "name"
4. git commit -m 'message'
5. git push -u origin master

对我来说,我必须确保公钥在服务器(附加在~/.ssh/authorized_keys中)和GitHub/Bitbucket(添加到GitHub或Bitbucket上的ssh密钥中)中正确配置-它们需要匹配。然后:

git add --all :/
git commit -am 'message'
git push -u origin master

如果您是第一次使用Git,则需要配置Git安装,包括:

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

git config --global user.name "Your Name"

在删除本地计算机上的所有文件后,我也出现了类似的错误,我必须清理存储库中的所有文件。

我的错误消息如下:

error: src refspec master does not match any.
error: failed to push some refs to 'git@github ... .git'

通过执行以下命令解决了问题:

touch README
git add README

git add (all other files)
git commit -m 'reinitialized files'
git push origin master --force  # <- caution, --force can delete others work.