git · Git
報錯原文
fatal: refusing to merge unrelated histories
這是什麼意思
你要合併的兩邊沒有任何共同祖先,在 git 看來它們是兩段互不相干的歷史。這通常發生在你本機用 git init 開始、提交了幾次,然後把一個已經有提交的遠端加為 origin 再 pull 的時候。加上 --allow-unrelated-histories 確實能合併,但它把兩段無關歷史硬焊在一個提交上,以後很難拆開;如果本機只有幾個提交,重新 clone 遠端再把檔案搬進去更乾淨。
怎麼修
git pull origin main --allow-unrelated-histories- 來自
- git
- Git
- 29
git 的錯誤幾乎都是一種拒絕,意思是「在目前狀態下做那件事會丟掉東西」;fatal: 表示它什麼都沒改就停了,而真正的解決辦法往往在第一行下面的 hint: 行裡。
怎麼讀報錯
- 從第一行往下讀。越往下越是工具內部的事,起因通常寫在最上面。
- 有檔名和行號就從那裡查——不是堆疊最上面那一格,而是最上面那條提到你自己寫的檔案的行。
- 把報錯原文照樣去搜,但先去掉你自己的路徑和變數名,正是那些讓搜尋比對不上。
- 同一種情況在不同版本裡措辭不同。結果不對頭,就把版本號一起加進查詢。
- 貼上修法之前,先確認它會丟掉什麼。這裡面有些是不能收回的。
常見問題
Q. “fatal: refusing to merge unrelated histories” 是什麼意思?
你要合併的兩邊沒有任何共同祖先,在 git 看來它們是兩段互不相干的歷史。這通常發生在你本機用 git init 開始、提交了幾次,然後把一個已經有提交的遠端加為 origin 再 pull 的時候。加上 --allow-unrelated-histories 確實能合併,但它把兩段無關歷史硬焊在一個提交上,以後很難拆開;如果本機只有幾個提交,重新 clone 遠端再把檔案搬進去更乾淨。
Q. 怎麼修?
git pull origin main --allow-unrelated-histories —— 執行前先看上面的說明,確認這條命令會丟掉什麼。
Q. 這是哪個工具報的?
git。它屬於Git,報錯原文有 6 個詞。
相關報錯
Updates were rejected because the remote contains work that you do not have locally.gitCONFLICT (content): Merge conflict in src/app.tsxgitfatal: not a git repository (or any of the parent directories): .gitgitYour branch and 'origin/main' have divergedgitfatal: Need to specify how to reconcile divergent branches.gitPlease commit your changes or stash them before you merge.giterror: Your local changes to the following files would be overwritten by checkout:gitYou are currently rebasing branch 'feature' on '8a3f21c'.git