You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何避免git clone --filter=blob:none --sparse下载根目录文件?

如何避免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

根目录下的文件(如示例仓库中的generate.sh)仍会被下载。要彻底避免这种情况,仅获取目标子目录,可通过以下两种方法实现:

方法1:关闭Cone模式并设置精确路径规则

Git默认的稀疏检出(sparse checkout)使用Cone模式,该模式会默认包含根目录文件。关闭Cone模式后,可设置更精确的路径匹配规则:

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
# 关闭Cone模式,启用精确路径匹配
git sparse-checkout init --no-cone
# 设置仅包含small目录下的所有内容
git sparse-checkout set 'small/**'

方法2:手动编辑稀疏检出规则文件

直接修改.git/info/sparse-checkout文件,先排除所有根目录内容,再明确包含目标子目录:

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
# 清空现有规则,设置先排除所有根目录文件,再包含small目录
echo "/*" > .git/info/sparse-checkout
echo "!/*" >> .git/info/sparse-checkout
echo "small/" >> .git/info/sparse-checkout
# 更新工作区以应用规则
git read-tree -mu HEAD

注意事项

  • 以上方法在Git 2.37.2版本(测试环境:Ubuntu 22.10,2023年2月)中验证有效。
  • 若根目录仅存在小文件,对下载速度影响有限;但如果根目录包含大文件,上述方法可有效避免不必要的下载。

内容的提问来源于stack exchange,提问作者Ciro Santilli OurBigBook.com

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 06:00:47