新建Github仓库并上传本地代码

按照Github的教程 Adding a local repository to GitHub using Git

1. 创建空的Github仓库

创建远程仓库 🔗 ,注意不要勾选 Add a README file,如果勾选会生成一笔提交,如果本地仓库也提交了代码,会导致拉取/推送代码失败。

2. 切换到本地环境,初始化仓库

$ git init -b main

3. 添加本地文件并提交

$ git add .
Adds the files in the local repository and stages them for commit. 若要取消暂存文件,请使用“git reset HEAD YOUR-FILE”。
$ git commit -m "First commit"
Commits the tracked changes and prepares them to be pushed to a remote repository. 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。

4. 关联本地仓库和远程仓库

$ git remote add origin  <remote_url>
Sets the new remote
$ git remote -v
Verifies the new remote URL</remote_url>

5. 推送代码到远程仓库

$ git push -u origin main
Pushes the changes in your local repository up to the remote repository you specified as the origin

如果勾选了 Add a README file ,执行最后一步,推送本地代码到远程仓库时会报错

error: failed to push some refs to 'github.com:bwz1984/sql2code.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

因此尝试拉取远程代码

$ git pull origin main

终端提示如下

hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint:
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.

fatal: refusing to merge unrelated histories

重点在于最后一句,Git提示 fatal: refusing to merge unrelated histories,原因分析如下

You have a new Git repository with some commits. You then try to pull from an existing remote repo. The merge becomes incompatible because the histories for branch and remote pull are different. Git sees the situation as you trying to merge two completely unrelated branches, and it doesn’t know what to do.

解决方案

$ git pull origin main --allow-unrelated-histories
$ git push origin main

Original: https://www.cnblogs.com/amos01/p/16671258.html
Author: Amos01
Title: 新建Github仓库并上传本地代码

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/613047/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • DM-DM之间的DBLINK

    1 创建外部链接 创建一个外部链接。 语法格式 CREATE [OR REPLACE] [PUBLIC] LINK STANDBY FIRST | PRIMARY ONLY | S…

    数据库 2023年6月11日
    068
  • 基于Redis&MySQL接口幂等性设计

    基于Redis&MySQL接口幂等性设计 欲把相思说似谁,浅情人不知。 幂等性即多次调用接口或方法不会改变业务状态,可以保证重复调用的结果和单次调用的结果一致。 前端重复提…

    数据库 2023年6月14日
    071
  • Java 8的新特性还不了解?快进来!

    能坚持别人不能坚持的,才能拥有你想拥有的。关注 &#x7F16;&#x7A0B;&#x5927;&#x9053;,让我们一起成长 哈喽,大家好,我是…

    数据库 2023年6月11日
    070
  • Linux–>vi和vim编辑器的基本操作

    vim编辑器介绍 vi或者vim就是对linux下的文本进行编辑的一种编辑器比如说a.cpp文件这种 Linux会内置vi文本编辑器 Vim可以简单的认为vi的增强版 Linux是…

    数据库 2023年6月14日
    074
  • Ansible简介

    Ansible 是一种常用的自动运维化工具,基于 python 开发,分布式,无需客户端,轻量级,配置语言采用 YAML。 模块化:调用特定的模块,完成特殊的任务。 2.Param…

    数据库 2023年6月14日
    0105
  • Spring Security OAuth 笔记

    1 单点登录 关于单点登录的原理,我觉得下面这位老哥讲的比较清楚,有兴趣可以看一下,下面我把其中的重点在此做个笔记总结 https://juejin.cn/post/6844904…

    数据库 2023年6月14日
    074
  • 2021长安杯wp

    案件背景 2021年4月25日,上午8点左右,警方接到被害人金某报案,声称自己被敲诈数万元;经询问,昨日金某被嫌疑人诱导果聊,下载了某果聊软件,导致自己的通讯录和果聊视频被嫌疑人获…

    数据库 2023年6月11日
    070
  • Springboot学习笔记(一)—— 安装

    springboot越来越流行了,相比较于springMVC,springboot采用了一种约定大于配置的理念,可以一键安装,一键运行,一键部署,内置tomcat,省去了一大堆配置…

    数据库 2023年6月9日
    088
  • Indian Math tech

    https://www.youtube.com/watch?v=2j0nHEy5y18 本文来自博客园,作者:ukyo–BlackJesus,转载请注明原文链接:htt…

    数据库 2023年6月11日
    070
  • Java 杂记

    记载 Java 杂项的笔记和零碎的笔记 Java 杂记 一. Java 特性 1.1 封装 属性私有化,提供公共的可以访问的方法 1.2 继承(extends) Java &amp…

    数据库 2023年6月11日
    075
  • 入门Python,看完这篇就行了!

    转载请注明出处❤️ 作者:测试蔡坨坨 原文链接:caituotuo.top/3bbc3146.html 你好,我是测试蔡坨坨。 众所周知,Python语法简洁、功能强大,通过简单的…

    数据库 2023年6月11日
    087
  • 享受时间 拥抱 Linux

    生活上小闲心 用 游戏 影音 音乐 可以尝试 Window or Macos. 初期代码生涯推荐全面拥抱 Linux , 耐下心少学点, 多享受光 邵雍· 《清夜吟》 月到天心处,…

    数据库 2023年6月9日
    073
  • 精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队

    上篇文章介绍了如何创建合适的MySQL索引,今天再一块学一下如何更规范、更合理的使用MySQL? 合理规范的使用MySQL,可以大大减少开发工作量和线上问题,并提升SQL查询性能。…

    数据库 2023年5月24日
    080
  • 分布式全局唯一ID

    方案一、UUID UUID的方式能生成一串唯一随机32位长度数据,它是无序的一串数据,按照开放软件基金会(OSF)制定的标准计算,UUID的生成用到了以太网卡地址、纳秒级时间、芯片…

    数据库 2023年6月9日
    088
  • idea tags

    总结IDEA开发的26个常用设置https://zhuanlan.zhihu.com/p/108172369idea跳转到指定行列快捷键https://blog.51cto.com…

    数据库 2023年6月11日
    076
  • 快速入门上手Markdown

    第一次接触 Markdown是写代码初期看很多大佬的 github,他们的项目一定会有一份文件叫 Readme.md的文件他们由一些简单美观的符号和汉字字母组成,编译之后成为一篇简…

    数据库 2023年6月11日
    079
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球