error: failed to push some refs to ‘github.com | 本地上传远程仓库,一次踩坑记录

背景

在本地创建了一个本地仓库,想和远程仓库关联起来以后方便管理,以前也弄过,没想到这次花了将近1个小时,还是踩到坑里了。

首先一顿行云流水:

  1. 在本地文件夹 git init 初始化仓库
  2. git add .
  3. git commit -m "first commit"
  4. 然后添加远程仓库:git remote add origin git@github.com:wingchi-leung/aBlog.git
  5. 然后push: git push -u origin master

然后打开一看,发现网站上有两个分支 main(默认)和master,提交到master分支上了,提示: main had recent pushes 2 minutes ago和一个compare & pull request 按钮。

想把提交的文件提交到main分支上,踩坑之路开始了:

首先瞎折腾合并不了, (对github网站操作还是不熟练)。

于是曲线救国:先备份、删仓库,重新走一遍开头的1,2,3,4
到了第5步,改成 git push -u origin main
报错: error: failed to push some refs to 'github.com:wingchi/aBlog.git'
最后在stakoverflow找到了答案:

main和master

以前的github,是将master作为默认分支的,但是最近改成了main,具体可以看这篇:https://github.com/github/renaming
大概原因是main是github最流行的分支名,也能支持各种翻译语言。。

原因

stackoverflow的答案是这样的:

By default and convention, the way we do this using git push is pretty simple:
git push origin main for instance.
The git push part is the command
that means send commits and ask them to set a name. The origin part is
what Git calls a remote: a short name that, mostly, holds a URL. The
main part at the end, here, is our branch name. That’s the one our Git
is using to find our commits. We’ll have our Git send our commits,
then ask their Git to set their main too.

This last part—where we’ve put in main here—is what Git calls a
refspec. Refspecs actually let us put in two names, separated by a
colon, or a couple of other forms. We can, for instance, use HEAD:main
as in Arka’s answer (although for technical reasons we might want to
use HEAD:refs/heads/main in many cases). But in simple cases, we can
just use one branch name: git push origin main. The simple branch name is a simple form of refspec.

大概意思是:

  • 默认情况下我们执行git push origin main, git push指的是发送提交请求,origin是一个url地址的简称.
  • main就是我们要提交的分支的名称。我们的git使用main这个分支名来找我们的分支,并且发送请求,要求其他的git将我们的main分支推送到他们的main分支上。

tip:git是分布式的,可以理解为很多台机器在使用这些git命令共享文件

  • refspec就是Git对这个main的叫法,实际上,更详细的表达应该是:HEAD:refs/heads/main

  • 所以,当我们输入git push origin main时,执行成功的关键是我们本地仓库要有main这个分支!

在本地创建的仓库,默认的分支是master,并且只有这么一个分支,那么当我们想推送的时候,git找不到我们的main分支,自然就报错了。

解决方法

将master分支名称改成main
git branch -m master main
然后再提交:git push -u origin main

其实本文的很多内容都是来自这里的https://stackoverflow.com/questions/65173291/git-push-error-src-refspec-main-does-not-match-any-on-linux
博主只是用三脚猫英语翻译然后用自己的理解表达出来,推荐英语好的直接看原文。

总结:实际上还是对git很多命令一知半解,对git的工作原理不熟悉
ps:stackoverflow上的答案详细多了,会英语的一点要多用它,少用某cxdx…

  • 12
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值