Git Shallow Clone 浅克隆与部分克隆
为什么要用 git clone 默认会把整个历史的所有对象都拉下来。有些场景只需要最新的一份代码,甚至只需要其中一个文件,例如 CI 里构建镜像、流水线里改一行 GitOps manifest 的镜像 tag。这时完整克隆既慢又浪费,网络差的时候还可能超时。 Git 提供了三种互相独立、可以叠加的裁剪方式: 方式 参数 裁掉什么 浅克隆 shallow clone --depth N 历史:只要最近 N 个 commit 部分克隆 partial clone --filter=blob:none 等 文件内容:blob 用到时再按需下载 稀疏检出 sparse checkout --sparse + git sparse-checkout 工作区:只检出指定路径 浅克隆 –depth # only the latest commit git clone --depth 1 https://github.com/foo/bar.git # --depth implies --single-branch; name the branch explicitly git clone --depth 1 --branch main https://github.com/foo/bar.git # by time instead of count git clone --shallow-since=2026-01-01 https://github.com/foo/bar.git --depth 默认带上 --single-branch,只拉一个分支。需要其它分支时,加 --no-single-branch。 ...