代码之家  ›  专栏  ›  技术社区  ›  Ilya Loskutov

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

  •  0
  • Ilya Loskutov  · 技术社区  · 5 年前

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

    $ 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 从借用树对象(和相关blob对象) 子树项目 (其 v1 国家)通过 read-tree 命令,则git可以将blob对象视为三维绘图的基础。

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

    0 回复  |  直到 5 年前
        1
  •  1
  •   jthill    5 年前

    为什么git在这里需要--allow无关历史标志?

    因为,据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
    
    推荐文章