自学内容网 自学内容网

github创建仓库并本地使用流程,以及问题src refspec xxx does not match any

1.在 GitHub 上创建一个新仓库

登录你的 GitHub 账户。
点击右上角的 “+” 按钮,然后选择 “New repository”。
填写仓库名称(如 my-repo)。
(可选)添加描述,选择是否公开或私有仓库。
(可选)选择初始化仓库的方式(如添加 README 文件、.gitignore 文件等)。
点击 “Create repository” 按钮。

2.本地创建git仓库

选择一个目录并创建一个新的文件夹用于你的项目
初始化一个新的 Git 仓库:

git init

3.连接本地仓库与远程仓库

git remote add origin https://github.com/yourusername/my-repo.git

4.添加文件并提交

git add .



git commit -m "xxx"

5.推送

git push

此时有问题:

fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

出现这个是因为:push命令用于将本地代码推送到远程仓库中,而”–set-upstream origin XXX“则用于设置本地分支与远程分支的关联,方便后续的代码提交和同步。

然而我们执行:git push --set-upstream origin master命令后

问题: error: src refspec xxx does not match any / error: failed to push some refs to …git

因为:现在建的 github 工程默认分支名为了 main,有博客提到由于受到"Black Lives Matter"运动的影响,GitHub 从今年 10 月 1 日起,在该平台上创建的所有新的源代码仓库将默认被命名为 “main”,而不是原先的"master"。但是我们本地创建的默认分支仍然是mater。故推测由于仓库分支名称不一样,导致远程分支和本地的仓库分支不能关联上

6.解决方案(亲测可用):

1.将本地分支master重命名为main,实现分支名相同
git branch -m master main

2.再执行  git push --set-upstream origin main

3.再执行
git push

成功

在这里插入图片描述

参考文档:Git 常见错误 之 error: src refspec xxx does not match any / error: failed to push some refs to 简单解决方法


原文地址:https://blog.csdn.net/weixin_44628096/article/details/142756399

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!