git - 如何初始化嵌套在子树中的子模块?

标签 git git-submodules git-subtree

显然,添加具有子模块的存储库的子树将破坏 git submodule init .这是一个重现问题的脚本:

#!/bin/sh
set -ex

mkdir submod
cd submod
git init
touch foo
git add foo
git commit -asm "This is a submodule"
cd ..

mkdir subtree
cd subtree
git init
git submodule add `realpath ../submod` submod
git commit -asm "This has reference to submodule"
cd ..

mkdir top
cd top
git init
touch bar
git add bar
git commit -asm "Dummy commit so HEAD resolves correctly"
git subtree add --prefix=subtree `realpath ../subtree` master

# This fails!
git submodule init

这个脚本正在做的是:
  • 创建一个 repo 子模块
  • 创建一个包含对 submod
  • 的子模块引用的 repo 子树
  • 创建一个 repo top,其中包含对子树
  • 的子树引用

    进一步考虑,问题出在哪里:子树机制在树中添加了子树的子模块对 submod 的引用,但是 .gitmodules元数据保留在 subtree/.gitmodules 中,而不是顶级 .gitmodules ,这意味着 git submodule init失败。如果我们复制 subtree/.gitmodules 的内容至.gitmodules ,相应地调整所有路径,解决问题......
    [submodule "submod"]
        path = subtree/submod
        url = /Users/ezyang/Dev/labs/git-subtree-submod/submod
    

    ...但是如果子树有很多子模块,那就有点痛苦了。有一个更好的方法吗?

    最佳答案

    从阅读文档和源代码可以看出... Git 子树与 Git 子模块完全分离,并且不会主动管理可能包含在子树项目本身中的任何子模块。
    如您所见,.gitmodules ,这对 git submodule init 的功能至关重要,需要在“主”存储库的根目录中维护(因为没有更好的术语)。

    Upon further consideration, it is clear what the problem is: the subtree mechanism has added subtree's submodule reference to submod to the tree, but the .gitmodules metadata remains in subtree/.gitmodules, not the top-level .gitmodules, which means that git submodule init fails. If we copy the contents of subtree/.gitmodules to .gitmodules, adjusting all the paths accordingly, that solves the problem...


    我强烈建议不要混合这些功能。

    关于git - 如何初始化嵌套在子树中的子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45422693/

    相关文章:

    macos - 无法克隆 git repo

    git-submodules - git submodule update 只需要最初?

    git 报告 merge 冲突,没有更改,空行(使用 git-subtree)

    Git:子树分割后从历史记录中删除不相关的提交

    git - 如何在 SourceTree.app 中设置头像?

    php - 更新实时网站的过程

    git - 删除 git 子模块 - 如何在 pull 时自动删除?

    git - 有没有办法将子模块添加到共享目录结构的git repo

    git - 使用分支增量提取子树

    html - Gitlist风格的git描述