Skip to main content

稀疏检出

开启 git 的稀疏检出特性

方法 1

git sparse-checkout init

方法 2

git config core.sparseCheckout true

方法 3

git sparse-checkout set /path/of/checkout/file

方法 4

git sparse-checkout add /path/of/checkout/file
注意

推荐使用方法 3方法 4方法1方法 3方法 4会自动生成 $GIT_DIR/info/sparse-checkout 文件,方法 3方法 4还会自动写入检出目录, 方法2需手动创建 $GIT_DIR/info/sparse-checkout 文件。

$GIT_DIR/info/sparse-checkout (稀疏检出文件)

稀疏检出文件用于定义跳过工作树引用位图。当 Git 更新工作目录时,它会基于此文件更新索引中的跳过工作树位。与文件中的模式匹配的文件将显示在工作目录中,其余文件不会。

查看稀疏检出文件列表

git sparse-checkout list

设置稀疏检出文件列表

git sparse-checkout set /path/of/checkout/file
注意

本命令会使用当前指定文件覆盖已有的检出文件配置。

新增稀疏检出文件

git sparse-checkout add /path/of/checkout/file
信息

本命令会将当前指定文件追加到已有的检出文件列表。

检出

git pull origin master

实战

mkdir checkout_dir && cd checkout_dir
git init && git sparse-checkout set /path/of/checkout/file
git remote add origin git_repository_url
git pull origin branch_name --depth 1
注意

pull 时指定 --depth 1 参数(Shallow Clone), 仅拉取最近的一次提交后的文件(包含所有文件,但是不包含所有的提交历史)。 可在 pull 时指定 --unshallow 参数拉取完整提交历史。

git fetch --unshallow