新建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)

大家都在看

  • LRU算法

    class LRUCahce { private Node head; private Node tail; private Map hashMap; private int ca…

    数据库 2023年6月14日
    084
  • Java并发编程之AQS以及源码解析

    文章目录 概览 实现思路 实现原理 * 源自CLH锁 AQS数据模型 CAS操作 主要方法 * 自定义同步器的实现方法 AQS定义的模板方法 源码解读 * 等待状态释义 AQS获取…

    数据库 2023年6月6日
    084
  • 记录一次docker镜像拉取失败的问题

    syslog日志 Mar 13 08:42:41 xxx dockerd[30691]: time=”2022-03-13T08:42:41.928436506Z&#8…

    数据库 2023年6月9日
    0116
  • Nginx负载均衡

    Nginx负载均衡 负载均衡概述 早期的网站流量和业务功能都比较简单,单台服务器足以满足基本的需求,但是随着互联网的发展,业务流量越来越大并且业务逻辑也跟着越来越复杂,单台服务器的…

    数据库 2023年6月6日
    088
  • 【数据结构】跳表

    一、基本概念 1.1 定义 跳表(SkipList):增加了向前指针的链表叫做指针。跳表全称叫做跳跃表,简称跳表。跳表是一个随机化的数据结构,实质是一种可以进行二分查找的有序链表。…

    数据库 2023年6月11日
    094
  • Nginx 反向代理、Rewrite

    Rewrite功能配置 Rewrite是Nginx服务器提供的一个重要基本功能,是Web服务器产品中几乎必备的功能。主要的作用是用来实现URL的重写。www.jd.com注意:Ng…

    数据库 2023年6月6日
    092
  • 史上最全Mysql规范

    1 整体规约 1)【强制】数据库所有对象必须要有注释,包括:表、字段、索引等,并且要保持最新; 1)【强制】默认使用utf8字符集,无乱码风险,除一些需要存储特殊符号的字段,可以采…

    数据库 2023年5月24日
    089
  • MySQL实战45讲 11

    11 | 怎么给字符串字段加索引? Q:如何在邮箱这样的字段上建立合理的索引? 用户表的定义: create table SUser( ID bigint unsigned pri…

    数据库 2023年5月24日
    0119
  • 设计模式遵循的设计原则

    一、什么是设计原则? 答:如果说设计模式是编写代码的一种套路,那么设计原则就是用来约束我们使用这种套路应该要遵循的规则,只有遵循了这些规则的设计模式编写出来的应用程序才具有更好的扩…

    数据库 2023年6月14日
    086
  • 我设计数据库常用的几个原则

    以MySQL5.7为例,在一个项目中的数据库schema中建表 〇、建库 统一字符集和排序规则 规则 库的默认字符集选择utf8mb4,表、字段默认上级 库的排序规则选择utf8m…

    数据库 2023年6月9日
    0108
  • Golang context

    Context Go 语言中提供了 context 包,通过显示传递 context, 实现请求级别的元数据、取消信号、终止信号的传递。context 包提供了从现有的上下文值(c…

    数据库 2023年6月16日
    090
  • mybatis-plus详细讲解

    本文笔记都是观看狂神老师视频手敲的,视频地址:https://www.bilibili.com/video/BV17E411N7KN 学java后端的都可以去看一下,从基础到架构很…

    数据库 2023年6月14日
    096
  • 从零开始的常用MySQL语句练习大全

    很多时候深入学习固然很重要,但是想要写下一篇给新手都能看得懂看的很香,并且老鸟可以查漏补缺的的练习博客,还是挺有难度,所以今天尝试写一些关于MySQL的语句练习大全,供想要从零开始…

    数据库 2023年6月11日
    091
  • MySQL高可用架构-MMM、MHA、MGR、PXC、分库分表(补总结)

    404. 抱歉,您访问的资源不存在。 可能是URL不正确,或者对应的内容已经被删除,或者处于隐私状态。 [En] It may be that the URL is incorre…

    数据库 2023年5月24日
    0111
  • 源码 | 为金融场景而生的数据类型:Numeric

    高日耀 资深数据库内核研发毕业于华中科技大学,喜欢研究主流数据库架构和源码,并长期从事分布式数据库内核研发。曾参与分布式 MPP 数据库 CirroData 内核开发(东方国信),…

    数据库 2023年5月24日
    0105
  • 汇编语言学习记录一

    0x00——什么是汇编语言? 汇编语言:早期实现程序员和机器进行交互的汇编指令集。 汇编指令,通过编译器,转换成机器码,从而使 机器 理解其指令。 0x01——汇编语言的组成 汇编…

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