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

大家都在看

  • MySQL存储过程和函数

    存储过程与函数 类似与Java的方法和C语言的函数 存储过程概述 含义 一组经过 预先编译的SQL语句的封装 执行过程:存储过程预先存储在MySQL服务器上,客户端发出命令后,服务…

    数据库 2023年5月24日
    0119
  • [springmvc]ssm框架整合超简单

    此整合没有具体的业务,因为ssm整合最难的点就在于配置文件的整合,因此这里只详细记录配置文件的整合 spring和dao整合将mybatis的配置文件的数据库连接和sqlsessi…

    数据库 2023年6月16日
    0128
  • String s = new String(“xyz”)创建了几个实例你真的能答对吗?

    从面试题说起 String s = new String("xyz"); 创建了几个实例? 这是一道很经典的面试题,在一本所谓的Java宝典上,我看到的&#82…

    数据库 2023年6月16日
    0154
  • MySQL锁(乐观锁、悲观锁、多粒度锁)

    锁 并发事务可能会发生什么情况: [En] What may happen to concurrent transactions: 读-读事务并发:此时是没有问题的,读操作不会对记…

    数据库 2023年5月24日
    0138
  • 在线安装Docker

    安装 yum-utils 包yum install -y yum-utils 设置存储库# 官方地址(比较慢) yum-config-manager \ –add-repo \ …

    数据库 2023年6月14日
    0149
  • 计科书单

    看来书单太长 两部分还是不够… 这一部分是计科的, 有计科概论、数据结构与算法、汇编、编译、数电、数字逻辑、计组、操作系统、计网等 计科概论计算机科学导论计算机科学概论…

    数据库 2023年6月11日
    0110
  • MySQL设计表结构

    时间datetime 创建时间不能自动更新,更新时间需要自动更新 CURRENT_TIMESTAMP:创建时,会用当前时间自动填充该字段值 CURRENT_TIMESTAMP ON…

    数据库 2023年6月9日
    0127
  • Consul 入门-运行

    HashiCorp Consul 是由 HashiCorp 公司开发的,它是一家专注于 DevOps 工具链的公司,旗下的明星级产品包括 Vagrant、Terraform、Vau…

    数据库 2023年6月6日
    0140
  • SkyWalking Agent端日志插件的编写历程与使用说明

    前一段时间顺利完成了SkyWalking Agent端logger-plugin插件的开发,在此做个总结。一方面给插件的使用方法写一中文说明,另一方面分享一下该插件开发过程中的一些…

    数据库 2023年6月11日
    0128
  • 太赞了!墙裂推荐这款网页版 Nginx 配置生成器,好用到爆!

    之前民工哥也给大家介绍过一款Nginx配置生成器:强大!Nginx 配置在线一键生成”神器”,不太了解的人可以去看一看。 最近民工哥又发现一款好用的网页版开…

    数据库 2023年6月9日
    0219
  • 回溯法套路总结与应用

    概述 回溯法常用于遍历一个列表元素的所有所有子集,比如全排列问题。可以说深度优先搜索就是回溯法的一种特殊形式。该方法的时间复杂度比较大一般为O(N!),它不像动态规划存在重叠子问题…

    数据库 2023年6月11日
    0151
  • 手写spring的ioc的流程截图(笔记-1)

    spring ioc是什么? IoC 容器是 Spring 的核心,也可以称为 Spring 容器。Spring 通过 IoC 容器来管理对象的实例化和初始化,以及对象从创建到销毁…

    数据库 2023年6月6日
    0129
  • 计算机操作系统(慕课版)思维导图

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    数据库 2023年6月16日
    0144
  • 三道MySQL联合索引面试题,淘汰80%的面试者,你能答对几道

    众所周知MySQL 联合索引遵循最左前缀匹配原则,在少数情况下也会不遵循(有兴趣,可以翻一下上篇文章)。 创建 联合索引的时候,建议优先把区分度高的字段放在第一列。 至于如何计算分…

    数据库 2023年5月24日
    0113
  • 什么是字节

    字节(byte):是计算机中数据处理的基本单位,用大写的B表示 Original: https://www.cnblogs.com/Icy01/p/16311502.htmlAut…

    数据库 2023年6月11日
    0176
  • docker-compose部署rocketmq

    docker-compose安装: Ubuntu下载docker-compose文件 sudo&#xA0;curl&#xA0;-L&#xA0;https:/…

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