gitee码云上传代码 1、在码云创建项目 2、本地git添加远程库与本地库的关联 3、本地提交 4、拉取远程更新 5、更新与本地合并 6、上传最新的代码

在码云上按照提示创建项目

2、本地git添加远程库与本地库的关联

git init
git remote add origin https://gitee.com/xidianzxm/mybatisplus.git
or
git remote add origin git@gitee.com:xidianzxm/mybatisplus.git

3、本地提交

git add .
git commit -m "first commit"

4、拉取远程更新

git fetch

5、更新与本地合并

MacBookPro:mybatisplus zhangxm$ git merge origin/master
fatal: 拒绝合并无关的历史
MacBookPro:mybatisplus zhangxm$  git pull origin master --allow-unrelated-histories 
来自 gitee.com:xidianzxm/mybatisplus
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 LICENSE      | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 README.en.md |  36 ++++++++++++++++++++++++++++
 README.md    |  37 ++++++++++++++++++++++++++++
 3 files changed, 274 insertions(+)
 create mode 100644 LICENSE
 create mode 100644 README.en.md
 create mode 100644 README.md

6、上传最新的代码

MacBookPro:mybatisplus zhangxm$ git push origin master
枚举对象: 49, 完成.
对象计数中: 100% (49/49), 完成.
使用 4 个线程进行压缩
压缩对象中: 100% (28/28), 完成.

问题

MacBookPro:mybatisplus zhangxm$ git pull origin master
来自 gitee.com:xidianzxm/mybatisplus
 * branch            master     -> FETCH_HEAD
fatal: 拒绝合并无关的历史
----------------------------------------------------------------------------------------
MacBookPro:mybatisplus zhangxm$ git merge origin master --allow-unrelated-histories
merge:origin - 不能合并
----------------------------------------------------------------------------------------
MacBookPro:mybatisplus zhangxm$ git push origin master
To gitee.com:xidianzxm/mybatisplus.git
 ! [rejected]        master -> master (non-fast-forward)
error: 推送一些引用到 'git@gitee.com:xidianzxm/mybatisplus.git' 失败
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。
----------------------------------------------------------------------------------------
MacBookPro:mybatisplus zhangxm$ git pull origin master
来自 gitee.com:xidianzxm/mybatisplus
 * branch            master     -> FETCH_HEAD
fatal: 拒绝合并无关的历史
----------------------------------------------------------------------------------------
MacBookPro:mybatisplus zhangxm$ git fetch origin
----------------------------------------------------------------------------------------
MacBookPro:mybatisplus zhangxm$ git merge origin/master
fatal: 拒绝合并无关的历史
----------------------------------------------------------------------------------------
MacBookPro:mybatisplus zhangxm$  git pull origin master --allow-unrelated-histories 
来自 gitee.com:xidianzxm/mybatisplus
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 LICENSE      | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 README.en.md |  36 ++++++++++++++++++++++++++++
 README.md    |  37 ++++++++++++++++++++++++++++
 3 files changed, 274 insertions(+)
 create mode 100644 LICENSE
 create mode 100644 README.en.md
 create mode 100644 README.md
MacBookPro:mybatisplus zhangxm$ git push origin master
枚举对象: 49, 完成.
对象计数中: 100% (49/49), 完成.
使用 4 个线程进行压缩
压缩对象中: 100% (28/28), 完成.
写入对象中: 100% (48/48), 9.78 KiB | 1.63 MiB/s, 完成.
总共 48(差异 2),复用 0(差异 0),包复用 0
remote: Powered by GITEE.COM [GNK-5.0]
To gitee.com:xidianzxm/mybatisplus.git
   49d1136..5889cb3  master -> master
MacBookPro:mybatisplus zhangxm$ 

本文讲的是把git在最新2.9.2,合并pull两个不同的项目,出现的问题如何去解决
如果合并了两个不同的开始提交的仓库,在新的 git 会发现这两个仓库可能不是同一个,为了防止开发者上传错误,于是就给下面的提示

fatal: refusing to merge unrelated histories

如我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传。
这时会发现 github 的仓库和本地的没有一个共同的 commit 所以 git 不让提交,认为是写错了 origin ,
如果开发者确定是这个 origin 就可以使用 --allow-unrelated-histories 告诉 git 自己确定

遇到无法提交的问题,一般先pull 也就是使用 git pull origin master 这里的 origin 就是仓库,
而 master 就是需要上传的分支,因为两个仓库不同,发现 git 输出 refusing to merge unrelated histories 无法 pull 内容

因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在 git pull 之后,这句代码是在git 2.9.2版本发生的,最新的版本需要添加 --allow-unrelated-histories 告诉 git 允许不相关历史合并

假如我们的源是origin,分支是master,那么我们需要这样写 git pull origin master --allow-unrelated-histories 如果有设置了默认上传分支就可以用下面代码

 git pull --allow-unrelated-histories 

这个方法只解决因为两个仓库有不同的开始点,也就是两个仓库没有共同的 commit 出现的无法提交。
如果使用本文的方法还无法提交,需要看一下是不是发生了冲突,解决冲突再提交