Git 只下载仓特定的几个 Git LFS 文件
推荐做法(最常用、最干净)
先克隆仓库,但跳过所有 LFS 文件下载(只拿指针)
GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/xxx/yyy.git
# 或者在新版 git clone 也支持 --filter=blob:none 但对 LFS 不一定完全等价
cd yyy
此时工作目录里所有的 LFS 文件都是小文本指针(几十字节),不会占用大空间。
只拉取你想要的特定 LFS 文件
# 方法 A:单个文件(最推荐)
git lfs pull --include "path/to/your-big-file.pth"
# 或
git lfs pull --include "models/llama-7b.gguf"
# 方法 B:多个文件(用逗号分隔,或多次执行)
git lfs pull --include "file1.bin,file2.bin,images/*.png"
# 方法 C:用通配符(最灵活)
git lfs pull --include "checkpoints/*.safetensors"
git lfs pull --include "data/*.parquet"
这条命令会:
- 只下载你指定的路径对应的 LFS 对象
- 把它们从
.git/lfs 真正解压(smudge)到工作目录对应位置
- 其他 LFS 文件仍然保持指针状态
(可选)全局或永久设置只拉部分文件
如果你经常只下载某些文件,可以在仓库里加配置:
# 只允许拉取某些路径(其他的一律不自动拉)
git config lfs.fetchinclude "models/*.gguf,*.safetensors"
# 或者反过来禁止某些路径
git config lfs.fetchexclude "*.ckpt,old-backups/*"
之后正常的 git pull、git checkout 就只会处理匹配的文件。
其他常见组合
已经 clone 过了,想补救只下几个文件
直接用上面第 2 步的 git lfs pull --include 即可。
想知道仓库里有哪些 LFS 文件(方便选路径)
git lfs ls-files
# 或更详细(带 OID)
git lfs ls-files -l
一次性下多个不连续的文件(最直接写法)
git lfs pull --include "path1/modelA.bin,path2/optimizer.pt,config/*.json"
Hugging Face 模型仓库常见写法(2025-2026 很流行)
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct
cd Llama-3.1-8B-Instruct
# 只下主模型权重(假设是 safetensors 格式)
git lfs pull --include "*.safetensors"
# 或者只下某几个分片
git lfs pull --include "model-00001-of-0000?.safetensors"
注意事项
--include 支持通配符 *、**(但要加引号避免 shell 展开)
--include 和 --exclude 可以同时用,规则是:先匹配 include,再过滤 exclude
- 如果你后续
git checkout 切换到其他 commit,可能会需要再次 git lfs pull 对应文件
- 极致省空间需求 → 可以考虑用
git clone --filter=blob:none + sparse-checkout 结合,但对纯 LFS 场景帮助有限(LFS 还是得手动 pull)
总结最简流程(几乎所有场景都适用):
GIT_LFS_SKIP_SMUDGE=1 git clone <url>
cd <repo>
git lfs pull --include "你要的文件路径1" --include "路径2" # 可多次 --include
# 或者直接写成:
# git lfs pull --include "path/*.bin,important/model.gguf"