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

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'

当前回答

面临的问题

当我在GitHub上创建一个新的存储库并将其与我的客户端计算机上的react应用程序链接时,我也遇到了同样的问题。

我使用了以下步骤:

出现问题之前使用的命令

git init
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

我的错误

但正如你所见,我的错误是没有使用gitadd。命令我犯了这个错误,因为我已经有了README.md文件,而GitHub在创建存储库时会指导我们使用基本命令。

我的解决方案

我的解决方案是使用gitadd。gitinit命令之后。

按照相同的顺序使用以下命令集来解决问题:

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

其他回答

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的错误:

错误:src-refspec-master与任何不匹配。

已修复此步骤:

git commit -m "first commit"

在我跑步之前:

git add <files>

在我跑步后:

git push -u origin master

如果您尝试推送的分支名称中有拼写错误,也会发生这种情况。

git add .

这就是你所需要的。该代码跟踪目录中所有未跟踪的文件。

两种可能性:

1-要么忘记包含.gitignore文件。

以下是所需的所有步骤:

在远程上创建一个空的Git存储库,在本地创建.gitignore文件。GitHub在这里列出了一些示例启动终端,然后在项目中执行以下命令:git remote添加原点YOUR/origin.gitgit添加。gitcommit-m“初始提交或首次提交的任何消息”git push-u原始主机

2-或者您正在尝试创建一个新的Github项目。

Github将master替换为main作为默认分支名称。要解决此问题,请执行以下操作:

在本地项目上:删除.git文件夹(如果存在)通过在项目中启动以下操作,重新创建一个干净的存储库:

在终端中:

git init

git add .

git commit -m "YOUR FIRST MESSAGE HERE"

git branch -M main

git remote add origin _GIT_LINK_TO_PROJECT_HERE_

git push -u origin main