1. 程式人生 > >當 git pull 碰到拒絕合併無關歷史

當 git pull 碰到拒絕合併無關歷史

問題描述

很久之前在 github 上建了個倉庫,裡面只有一個 README.md 檔案。
突然有天,我想把本地的一個專案傳上去,然後就碰到了這樣一個問題!
當我 新增遠端倉庫 後準備提交程式碼時,git 提示我請先使用 git pull
沒毛病,老鐵,就應該這樣!
但當我 git pull origin master 時就出現了這樣一條錯誤:

fatal: refusing to merge unrelated histories  // 拒絕合併無關歷史

解決辦法

在拉取時使用以下命令:

git pull origin master --allow-unrelated
-histories

對此,git 的 官方文件 是這樣描述的:

By default, git merge command refuses to merge histories that do not share a common ancestor. This option can be used to override this safety when merging histories of two projects that started their lives independently. As that is a very rare occasion, no configuration variable to enable this by default exists and will not be added.

預設情況下,git合併命令拒絕合併沒有共同祖先的歷史。當兩個專案的歷史獨立地開始時,這個選項可以被用來覆蓋這個安全。由於這是一個非常少見的情況,因此沒有預設存在的配置變數,也不會新增。(有道翻譯)

總結

本文所提到的情況只是一個很簡單的問題,--allow-unrelated-histories 似乎並不能解決所有人的麻煩。
比如這位網友碰到的問題 Git沒有共同祖先的兩個分支如何合併 , 遠端倉庫和本地的差異很零散而且似乎都很重要,使用 --allow-unrelated-histories 合併出的結果可能就會不盡人意,所以還要具體情況具體分析嘍!

參考