git 稀疏检出和部分克隆

Git>=2.25.0

1. Git 稀疏检出(Sparse Checkout)

  1. git sparse-checkout init --cone

这个命令用于初始化稀疏检出,并采用"--cone"模式。在"--cone"模式下,Git会自动根据当前所处的目录结构将存储库中的文件和目录纳入稀疏检出范围。换句话说,Git会根据当前检出的内容自动调整仅拉取所需的内容。

  1. git sparse-checkout set <dir1> <dir2> ...

这个命令用于手动配置稀疏检出,你可以在其中指定希望检出的文件或目录。你可以列出多个目录以及使用模式匹配来指定需要的内容。这个命令允许你有更精细的控制,只拉取你感兴趣的特定文件或目录,从而节省时间和磁盘空间。

2. Git 部分克隆(Partial Clone)

  1. git clone --filter=blob:none <url>

这个命令执行的是一种克隆操作,但是在克隆时不获取任何文件的内容,只获取提交历史。这在你只关心提交历史,而不需要文件内容的情况下非常有用。这样可以大大减少克隆所需的时间和带宽,尤其是对于大型存储库来说。

  1. git clone --filter=tree:0 <url>

这个命令也执行的是一种克隆操作,但是与上一个命令不同的是,它获取的是文件树的信息,而不是文件的内容或提交历史。这意味着你可以看到目录结构和文件名,但是文件的内容并没有被获取。同样,这在你只关心文件结构而不需要文件内容或提交历史的情况下非常有用。

  1. git clone --depth=1 <url>

这个命令执行的是一种浅克隆操作,它会将克隆的历史深度限制为指定的数量(这里是1)。这意味着你只会得到最近的一个提交及其相关内容,而不是整个历史记录。这对于快速获取最新版本的存储库非常有用,但是需要注意的是,由于历史记录被截断了,你可能无法进行某些操作,比如查看或回溯更早的提交。

场景1:克隆远程仓库

bash 复制代码
$ git clone --no-checkout https://github.com/derrickstolee/sparse-checkout-example 
Cloning into 'sparse-checkout-example'... 
remote: Enumerating objects: 14, done. 
remote: Counting objects: 100% (14/14), done. 
remote: Compressing objects: 100% (12/12), done. 
remote: Total 1901 (delta 1), reused 11 (delta 1), pack-reused 1887 
Receiving objects: 100% (1901/1901), 170.91 MiB | 74.79 MiB/s, done. 
Resolving deltas: 100% (181/181), done. 

$ cd sparse-checkout-example/ 
$ git sparse-checkout init --cone 
$ git checkout main 
$ ls 
bootstrap.sh LICENSE.md README.md
bash 复制代码
$ git clone --filter=blob:none --no-checkout https://github.com/derrickstolee/sparse-checkout-example 
Cloning into 'sparse-checkout-example'... 
Receiving objects: 100% (373/373), 75.98 KiB | 2.71 MiB/s, done. 
Resolving deltas: 100% (23/23), done. 

$ cd sparse-checkout-example/ 
$ git sparse-checkout set --cone 
$ git checkout main 
remote: Enumerating objects: 2, done. 
remote: Counting objects: 100% (2/2), done. 
remote: Compressing objects: 100% (2/2), done. 
remote: Total 3 (delta 0), reused 1 (delta 0), pack-reused 1 
Receiving objects: 100% (3/3), 1.41 KiB | 1.41 MiB/s, done. 
Already on 'main' Your branch is up to date with 'origin/main'. 
$ git sparse-checkout set client/android 
remote: Enumerating objects: 3, done. 
remote: Counting objects: 100% (3/3), done. 
remote: Compressing objects: 100% (3/3), done. 
remote: Total 26 (delta 0), reused 1 (delta 0), pack-reused 23 
Receiving objects: 100% (26/26), 985.91 KiB | 13.69 MiB/s, done.
相关推荐
ruanCat2 分钟前
对 changelogen 和 changelogithub 使用的思考
前端·github
行走的陀螺仪37 分钟前
git-旧项目继续开发新功能,同时还要维护线上版本
git·git push·git commit·git 协同开发
fendouweiqian5 小时前
git提交与commitlint规则
git
散峰而望10 小时前
C/C++输入输出初级(一) (算法竞赛)
c语言·开发语言·c++·算法·github
C.咖.13 小时前
Linux环境下——Git 与 GitHub
linux·git·github·远程仓库
CoderJia程序员甲15 小时前
GitHub 热榜项目 - 日榜(2025-11-12)
ai·开源·大模型·github·ai教程
杂鱼豆腐人15 小时前
pnpm环境下防止误使用npm的方法
前端·git·npm·node.js·git bash
摇滚侠16 小时前
GIT版本管理工具轻松入门 | TortoiseGit,Git 介绍软件安装配置,笔记01
笔记·git
mortimer17 小时前
只需三步,动手搭建一个本地免费【实时语音转录】工具WhisperLiveKit
ffmpeg·github·openai
山有木兮木有枝_18 小时前
“误操作导致 feat 功能未生效,尽管 merge 已完成”
git