1、在碼雲創建項目
在碼雲上按照提示創建項目
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 出現的無法提交。
如果使用本文的方法還無法提交,需要看一下是不是發生了沖突,解決沖突再提交