如 How do I clone a subdirectory only of a Git repository? 中所述到目前为止,我发现仅下载 Git 子目录中的所有文件的最佳方法是:
git clone --depth 1 --filter=blob:none --sparse \
https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git sparse-checkout set small
这是我迄今为止仅下载 small/
目录的最佳尝试。
但是,当我运行时:
git clone --depth 1 --filter=blob:none --sparse \
https://github.com/cirosantilli/test-git-partial-clone-big-small
根目录中存在的任何文件(但不是目录)都会被下载并出现在存储库中,在该测试存储库的情况下,我得到不需要的文件:
generate.sh
如何防止这种情况发生,仅获取我感兴趣的子目录,而不获取任何根目录文件?
我检查了其他存储库,例如https://github.com/torvalds/linux ,并且顶层有大量小文件不会显着减慢下载速度(通过单独下载它们),因此只有顶层有大文件时这才会成为问题。
已于 2023 年 2 月在 Git 2.37.2、Ubuntu 22.10 上测试。
最佳答案
进行克隆 --no-checkout
又名 -n
,然后完全按照您的需要设置稀疏规则。要获得真正最小的克隆流量,请不要使用 blob:none
,而使用 tree:0
。冒烟测试:
git clone -n --depth=1 --filter=tree:0 \
https://github.com/cirosantilli/test-git-partial-clone-big-small
cd !$:t:r
git sparse-checkout set --no-cone '*/'
git checkout
关于git - 如何防止 git clone --filter=blob :none --sparse from downloading files on the root directory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75311408/