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