“Updates were rejected.....“从本地Git仓库上传到GitHub的时候的问题和解决办法(helloFlask入门教程笔记)

Flask入门教程HelloFlask地址
没有直接在本地建好仓库再push到GitHub而是两边都建立了仓库然后链接了一下

github建立仓库直接在GitHub的UI界面自己建立就行

本地建立仓库
#在项目文件夹根目录下执行
git init 
#创建.gitignore文件
nano .gitignore
#在nano编辑界面写入
#写入内容如下:
*.pyc
*~
__pycache__
.DS_Store
#Control+O和Enter保存,Control+X退出
通过SSH密钥建立本地仓库与GitHub账户的链接
#检查是否已经创建密钥:
cat ~/.ssh/id_rsa.pub
#若显示"No such firle or directory",则执行 ssh-keygen 生成密钥,一路enter
#然后再次执行cat查看指令
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3Nza *******很多很多字符= Alan@Alan(最后显示了一下名字) 
#然后复制其文件内容到githubde SSH设置界面

# 从github新建的仓库的code下拉框里复制其SShURL "git@github.com:*******"
git remote add origin git@github.com:******* #星号为GitHub用户名和仓库名
提交commit
#将本地git仓库的默认分支也改成main
#因为GitHub现在新建的仓库直接默认是main而不是master
git config --global init.defaultBranch main
git add .
git commit -m "一个message"
git push origin main #推到远程GitHub仓库

然后问题就出现了,问题就在于。。。我在GitHub的网页界面给远程仓库新建了readme.md 然后发现再次从本地提交commit的时候,push会出错

出错情况:
To github.com:*********git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:********.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决办法

Stack Overflow中有一个答案可以解决问题,即由于出错原因在于远程仓库有新建文件,但是本地没有,存在冲突

在这里插入图片描述

具体我自己的操作如下,解决问题

#把远程仓库新建的文件拉下来合并
$ git pull origin main
From github.com:*************
 * branch            main       -> FETCH_HEAD
Merge made by the 'ort' strategy.
 README.md | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md
(env)
#把本地Git推到远程,完成提交
$ git push origin main
To github.com:**********
   59fa362..82f8a3a  main -> main
(env)
#我在Python的虚拟环境中所以有一个命令行的(env)的后缀
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值