(Git) 稀疏检出(Sparse Checkout) 拉取指定文件

发布于:2025-05-24 ⋅ 阅读:(14) ⋅ 点赞:(0)

🏭作用

类似于 .gitignore 进行文件的规则匹配。

一般在需要拉取大型项目指定的某些文件时进行使用。

🏭指令总览

git init

git config core.sparseCheckout true

# 生成稀疏规则文件
# .git/info/sparse-checkout
git sparse-checkout init

git remote add origin <url 地址>

git pull origin <分支名>

git config --list

👷core.sparseCheckout

需要打开本地 git 当前的稀疏检出开关。

# 打开
git config core.sparseCheckout true
# 关闭
git sparse-checkout disable

👷sparse-checkout 文件

用于生成对应的规则文件。当然自己手动建立也是可以的。

git sparse-checkout init

执行后会生成一个名为 sparse-checkout 的文本文件。

# 自动生成的默认都不同步
/*
!/*/

直接手动在这个文件中进行规则编辑即可。

路径规则举例

  • /script/ 仅匹配根目录下的 script
  • script/ 匹配所有同名目录(如 src/script/)。
  • 支持通配符(如 script/*.sh)和排除项(如 !script/temp/)。

若后续修改了 .git/info/sparse-checkout 文件,需运行下面指令进行数据同步。

git read-tree -mu HEAD

🏭实例演示

在这里插入图片描述

查看当前的基础配置项:(此处仅展示部分)

core.sparsecheckout=true 最为关键。

$ git config --list

diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/mySoftware/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager-core
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
credential.https://gitee.com.provider=generic
credential.http://192.168.10.223:8081.provider=generic
gui.encoding=utf-8
credential.http://192.168.10.220.provider=generic
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.sparsecheckout=true
remote.origin.url=git@gitee.com:cuber-lotus/python-code.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

⭐END

🌟交流方式

关注我,学习更多C/C++,python,算法,软件工程,计算机知识!

⭐交流方式⭐ |C/C++|算法|设计模式|软件架构-CSDN社区

B站

👨‍💻主页:天赐细莲 bilibili


网站公告

今日签到

点亮在社区的每一天
去签到