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

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及以后出现此错误,请在使用git init之前使用此命令

git-config--全局init.defaultBranch main这告诉您的git始终使用main作为默认分支名称,而不是master

其他回答

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 push-u原始主机

error: src refspec master does not match any.
error: failed to push some refs to 'http://REPO.git'

这是由于存储库仍然是空的。存储库中没有提交,因此没有主分支可以推送到服务器。

这对我有用

决议

1) git init
2)git commit -m "first commit"
3)git add app
4)git commit -m "first commit"
5)git push -u origin master

2020年:

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

确保分支名称中没有不可见的拼写错误:

git branch

输出:

master
* <U+0096>your_branch_name

有一个不可见的<U+0096>字符,可能是通过粘贴分支名称到达的。

将当前分支重命名为正确的值,然后重试推送:

git branch -m your_branch_name

2020年底,GitHub将其主分支更改为主分支。

我注意到GitHub创建了一个新的分支master,当我使用git push-u origin master时,这不是主分支:

现在,当我尝试使用gitpush-uorigin-main来直接推送到main分支时,会出现以下错误:

我面临这个错误:

src refspec main does not match any
error: failed to push some refs to 'https://github.com/<my_project_name>.git

在我第一次提交到main之后,我使用以下步骤修复了它。在以下代码中更改GitHub的URL:

git branch -M main
git remote add origin https://github.com/Sidrah-Madiha/<my_project_url>.git
git push -u origin main