新建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
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
新建Github仓库并上传本地代码的更多相关文章
- 在github上新建一个仓库并上传本地工程
扫盲:在github上新建一个仓库并上传本地工程 http://1ke.co/course/194 我自己新建了个项目,一步一步流程如下. zhoudd@desay:~/桌面/mini_embed_d ...
- github 仓库共享上传权限
https://blog.csdn.net/qq_33210042/article/details/79717497 打开仓库 -> Settings -> Collaborators 然 ...
- 【GIT】Github上传本地代码详解
本教程结合Github服务端和客户端完成本地代码上传至Github,下面进行详细讲解: 1.创建Github账号,这一个步骤应该不用太多解释,直接上官网进行注册登录即可https://github.c ...
- 上传本地代码及更新代码到GitHub教程
上传本地代码及更新代码到GitHub教程 上传本地代码 第一步:去github上创建自己的Repository,创建页面如下图所示: 红框为新建的仓库的https地址 第二步: echo " ...
- git配置SSH Key,上传本地代码至github
git配置全局的name和email git config --global user.name "name" git config --global user.email &qu ...
- github小白上传本地代码及更新代码到GitHub及华为云教程
上传本地代码 第一步:去github上创建自己的Repository,创建页面如下图所示: 红框为新建的仓库的https地址 第二步: echo "# Test" >> ...
- 【Hyeri】上传本地代码到Github
个人摸索出的上传本地代码到Github的办法 首先配置SSH Key(首次需要配置) 1.设置本地ssh key,打开git bash,输入命令: ssh -keygen -t rsa -C &quo ...
- mac 上传本地代码到 Github 教程
网上有很多关于windows系统上传本地代码到github的文章,但是自己用的是mac,在网上也找了相关文章,实践的过程中还是遇到了很多问题,现在把自己的成功实践分享出来,希望能对大家有帮助. 1.首 ...
- (超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库
(超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库 本地创建了一个 xcode 工程项目,现通过 命令行 将该项目上传到 github 或者 gitlab 远程仓库,具体 ...
随机推荐
- Linux文本三剑客-sed
sed工作原理: sed: Stream Editor.流编辑器 --- 属于行编辑工具 sed和vim一样都是文本编辑工具. 行编辑工具:一行一行处理文件内容 全屏编辑工具:一次性将文件内容加载到内 ...
- 全网求解,用Python处理一个基础题目
昨天在群里看见一个Python的问题,趁着今天有那么一点点时间,就想把这个题目分享出来,让大家一起解决.毕竟三个臭皮匠,赛过诸葛亮.原始数据如下: 1 origin_lst = [0, 0, 1, 2 ...
- key可重复的Map
在正常的map操作中,key是不能重复的,如果希望key的内容可以重复,可以用IdentityHashMap 举个栗子 输出结果: public static void main(String[] a ...
- LeetCode. 812. 最大三角形面积
812. 最大三角形面积 鞋带公式 鞋带公式,用于计算任意多边形的面积,可用于计算三角形的面积 已知 ΔABC 三个顶点的坐标 A:(x1,y1). B:(x2,y2). C:(x3,y3) 对应的矩 ...
- 强化学习-学习笔记4 | Actor-Critic
Actor-Critic 是价值学习和策略学习的结合.Actor 是策略网络,用来控制agent运动,可以看做是运动员.Critic 是价值网络,用来给动作打分,像是裁判. 4. Actor-Crit ...
- labview从入门到出家3--制作和调用子VI
当程序越写越大的时候,我们会发现代码界面会比较乱(线太多),那要怎么做可以让代码更简洁一点,我只管直接调用某个功能函数,而不需要在一个VI上面去实现这个功能函数呢?--子VI.好比C语言里面的Main ...
- PoweJob高级特性-MapReduce完整示例
由于网上搜索 PowerJob MapReduce 都是设计原理,demo也展示个空壳子,没有演示Map到Reduce结果怎么传递,对于没有MR开发经验的人来说并没有什么帮助,所以这里写了一个有完整计 ...
- 方法的调用和JDK9的JShell简单使用
方法在定义完毕后,方法不会自己运行,必须被调用才能执行,我们可以在主方法main中来调用我们自己定义好的方法.在主方法中,直接写要调用的方法名字就可以调用了 public static void ma ...
- @Async注解的坑,小心
大家好,我是三友. 背景 前段时间,一个同事小姐姐跟我说她的项目起不来了,让我帮忙看一下,本着助人为乐的精神,这个忙肯定要去帮. 于是,我在她的控制台发现了如下的异常信息: Exception in ...
- surging作者出具压测结果
前言 首先回应下@wen-wen 所贴的压测报告,我也把我和客户压测碰到的问题,和压测结果贴出来,这个结果是由客户提供的.不会有任何的舞弊手脚问题 问题一:Task.Run慎用 首先在最新的社区版本已 ...