我有一个用于稀疏结账的子模块。如果没有稀疏的结帐,我会这样做
git submodule update --init <path/to/submodule>
但是如何将子模块存储库初始化为空而不获取它(或获取它,但不签出),以便我可以调用
cd <path/to/submodule>
git config core.sparseCheckout true
cd <../../..>
echo <subdir-to-get> .git/modules/<path/to/submodule>/info/sparse-checkout
不幸的是
git submodule init <path/to/submodule>
不会在
.git/modules/<path/to/submodule>
中创建存储库和文件 <path/to/submodule>/.git
。
您可以尝试,如此处,先将子模块克隆为普通存储库,然后使用
git submodule absorbgitdirs
。
首先以深度为 1 进行克隆,您不会获得太多数据:
git clone --depth=1 --no-checkout an/Url <path/to/submodule>
git submodule add an/Url <path/to/submodule>
git submodule absorbgitdirs
然后,您可以修改
.git/modules/<path/to/submodule>/info/sparse-checkout
git -C <path/to/submodule> config core.sparseCheckout true
echo 'foo/*' >>.git/modules/<path/to/submodule>/info/sparse-checkout
最后,只获取你想要的文件:
git submodule update --force --checkout <path/to/submodule>
正如 Janus 所评论,使用 git clone --filter
git clone --filter
选项的语法是什么?”进行了说明:
#fastest clone possible:
git clone --filter=blob:none --no-checkout https://github.com/git/git
cd git
suburl=$(git config -f .gitmodules --get submodule.<sub>.url)
git submodule update --init --force --checkout <sub>
(将 <sub>
替换为
.gitmodules
中的子模块名称条目)
使用过滤器初始化现有子模块:
git submodule update --init --filter=blob:none
使用过滤器添加子模块:
git clone --filter=blob:none sub/mod/url [submodpath]
git submodule add sub/mod/url [submodpath] # doesn't download
git submodule absorbgitdirs