git 可以猜猜在进行子树合并时合并基数是什么并且没有共同的祖先吗?

Can git make a guess at what's the merge base when doing a subtree merge and there are no common ancestors?

我正在努力了解子树合并。让我们需要将一个项目用作另一个项目的子树,然后有机会通过获取工具更新第一个项目(反之亦然)。我们的工作流程将是这样的:

$ git init subtree-project
$ cd subtree-project
$ echo foo > foo.TXT
$ git add .
$ git commit -m v1
$ cd ..
$ git init main-project 
$ cd main-project
$ echo boo > boo.TXT
$ git add .
$ git commit -m v1
$ git remote add subtree ../subtree-project
$ git fetch subtree
$ git branch subtree-project-master subtree/master
$ git read-tree --prefix=subtree-directory -u subtree-project-master
$ git commit -am v2 #save a subtree in the master branch

然后 subtree-project 被更新 (v2)。将新提交拉入 subtree-project-master 分支后,我将其作为子树 (subtree-directory) 合并到 master 分支中:

$ git merge --squash -s recursive -Xsubtree=subtree-directory subtree-project-master
fatal: refusing to merge unrelated histories

这里为什么git需要--allow-unrelated-histories标志?毕竟,我们使用的是子树合并,git默默地完成快进合并还不够吗?

只是为了澄清我的问题:据我了解子树合并的概念,git在这种情况下可以猜测什么可以被视为合并对象的基础 blob 对象。也就是说,如果 main-project 通过 read-tree 命令从 subtree-project(它的 v1 状态)借用了一个树对象(和相关的 blob 对象),那么 git可以认为 blob 对象是 3-diffing 的基础。

我几乎可以肯定这是一个误会,但仍需要一些澄清。

Why does git need the --allow-unrelated-histories flag here?

因为,就 git 而言,这些历史是不相关的。您的原始插入未记录为合并,它是使用无祖先读取树完成的。

而是记录合并,

git fetch subtree
# do the first one as a handroll to show git where things go
git merge -s ours --no-commit subtree/master --allow-unrelated-histories
git read-tree -u --prefix=subtree-directory
git commit

现在 git 看到你在做什么,你可以告诉它再做一次:

[... time passes, subtree project changes ...]
git fetch subtree
git merge -s subtree subtree/master