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

大家都在看

  • jmeter的一些概念知识

    前言 一、Jmeter的作用 – 1.jmeter进行接口操作 2. jmeter进行性能操作 二、Jmeter的一些概念的理解 – 1.事务 2. TPS…

    数据库 2023年6月6日
    0111
  • 0812Java核心技术卷(1)随笔

    自增运算符与自减运算符 这些运算符改变了变量的值,所以它的操作数不能是数值。例如4++就是一条非法语句不建议在其他表达式内部使用++,因为这样会降低代码可读性,产生bug Orig…

    数据库 2023年6月14日
    0105
  • visual studio 2015 IOS开发连接mac时提示错误couldn’t connect to xxxx, please try again的一个方法

    本人使用虚拟机MAC。原本使用虚拟机中的VS2015连接正常没有问题。 但是当把MAC的虚拟机文件COPY到另一个机器上,提示”couldn’t conne…

    数据库 2023年6月14日
    093
  • SQL语句大全–SQL

    前言 本片博客使用mysql数据库进行数据操作,使用Navicat for mysql 这个IDE进行可视化操作。每个SQL语句都是亲身实验验证的,并且经过自己的思考的。能够保证s…

    数据库 2023年5月24日
    065
  • macbook air 2019 安装win10单系统

    目前不考虑写的太详细了,如果有同学遇到问题了我再完善,主要是把遇到的坑讲下第一步,准备2个U盘(如果不嫌麻烦一个也可以)1.用大白菜或者老毛桃将其中一个做成启动盘2.在window…

    数据库 2023年6月9日
    0345
  • 程序员“迷惑代码”大赏

    谈到程序员,对于外行人来说一贯的印象就是格子衫大裤衩外加人字拖,蓬头(秃头)垢面黑眼圈,还有就是”人傻钱多死得快”🤣,这是外界对程序员固有的思想,但是作为新…

    数据库 2023年6月11日
    099
  • Linux Centos 打开和关闭防火墙

    systemctl status firewalld.service # 查看防火墙状态 systemctl start firewalld.service # 开启防火墙 sys…

    数据库 2023年6月14日
    0106
  • IDEA中如何查看接口的所有实现类呢?

    接口是我们日常开发中常用的操作,那么如何查看一个接口有哪些实现类呢?下文笔者将讲述IDEA编辑器中 查看实现类的快捷方法,如下所示 在spring源码阅读中,每一个接口都有很多实现…

    数据库 2023年6月11日
    084
  • ASP.NET CORE WEB项目介绍

    首先创建一个asp.net core web应用程序 第二步 目前官方预置了7种模板项目供我们选择。从中我们可以看出,既有我们熟悉的MVC、WebAPI,又新添加了Razor Pa…

    数据库 2023年6月14日
    097
  • windows下安装mysql5.7

    1.首先官网下载ZIP安装包(即以解压,配置的方式安装) 2.解压完成之后在目录下创建 my.ini文件 内容如下: [mysql]设置mysql客户端默认字符集default-c…

    数据库 2023年5月24日
    087
  • zabbix自定义监控进程和日志

    自定义监控 进程 日志 mysql主从状态 mysql主从延迟 自定义监控 进程 [root@client ~]# cd /usr/local/etc/ [root@client …

    数据库 2023年6月14日
    096
  • qt项目设置程序图标

    一、下载好.ico格式的图标文件并存放到项目的根目录这里附赠一个png,jpg等格式转为ico格式的网站 二、在.pro文件里面添加ico的名字 三、发现在debug模式下运行项目…

    数据库 2023年6月6日
    0101
  • 局域网内访问子网服务(访问电脑虚拟机中的服务)

    局域网内访问子网服务 问题描述: 同一个路由器(172.18.0.0)下面有两台电脑A(172.18.40.45)和B (172.18.44.173) ,在B电脑上安装虚拟机 ,使…

    数据库 2023年6月9日
    0101
  • B树-查找

    B树系列文章 1. B树-介绍 2. B树-查找 3. B树-插入 4. B树-删除 查找 假设有一棵3阶B树,如下图所示。 下面说明在该B树中查找 52的过程 首先,从根结点出发…

    数据库 2023年6月14日
    0144
  • Windows 是最安全的操作系统

    建了一个用户交流群,我在群里说:”Windows 是最安全的操作系统。” 立刻引发了很多有意思的观点。我在群里一个人说不过大家,先篇文章把自己的论点罗列一下…

    数据库 2023年6月6日
    0257
  • 使用 GitHub 协同开发规范

    记一下项目开发的规范,统一开发规范可以有效提高共同开发效率和代码质量 本文档图片的 winsoullin 理解为团队正式发布的仓库,Nightnessss 理解为个人仓库 Fork…

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