显然,添加具有子模块的存储库的子树将破坏 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
这个脚本正在做的是:
进一步考虑,问题出在哪里:子树机制在树中添加了子树的子模块对 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/