0
0
Gitdevops~7 mins

Sparse checkout for partial repos in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes repositories are very large, but you only need a small part of the files. Sparse checkout lets you download and work with only the parts you want, saving space and time.
When you want to clone a huge repository but only need one folder or file inside it.
When your internet is slow and downloading the full repo would take too long.
When your computer has limited storage and cannot hold the entire repository.
When you want to work on a specific module or feature without distractions from other files.
When you want to speed up build or test processes by only checking out relevant code.
Commands
Clone the repository without checking out files. This prepares the repo for sparse checkout.
Terminal
git clone --no-checkout https://github.com/git/git.git my-git-repo
Expected OutputExpected
Cloning into 'my-git-repo'... remote: Enumerating objects: 100000, done. remote: Counting objects: 100% (100000/100000), done. remote: Compressing objects: 100% (50000/50000), done. Receiving objects: 100% (100000/100000), 50.00 MiB | 2.00 MiB/s, done. Resolving deltas: 100% (70000/70000), done.
--no-checkout - Prevents automatic checkout of files after cloning
Change directory into the cloned repository to run further git commands.
Terminal
cd my-git-repo
Expected OutputExpected
No output (command runs silently)
Enable sparse checkout mode with cone pattern support for easier folder selection.
Terminal
git sparse-checkout init --cone
Expected OutputExpected
Sparse checkout initialized.
--cone - Simplifies sparse patterns to folder-based rules
Specify the folder 'Documentation' to be checked out. Only this folder will be downloaded.
Terminal
git sparse-checkout set Documentation
Expected OutputExpected
Updated working directory.
Checkout the main branch to populate the working directory with only the specified sparse files.
Terminal
git checkout main
Expected OutputExpected
Updating files: 100% (500/500), done.
List files and folders in the current directory to verify only the 'Documentation' folder is present.
Terminal
ls
Expected OutputExpected
Documentation
Key Concept

Sparse checkout lets you work with only parts of a repository by downloading just the files or folders you need.

Common Mistakes
Cloning the repo normally without --no-checkout and then trying to enable sparse checkout.
Git will have already checked out all files, so sparse checkout won't reduce files on disk.
Use 'git clone --no-checkout' first, then enable sparse checkout before checking out files.
Not running 'git sparse-checkout init' before setting sparse paths.
Sparse checkout is not enabled, so setting paths has no effect.
Always run 'git sparse-checkout init' to enable sparse checkout mode first.
Using complex sparse patterns without --cone mode when only folders are needed.
Complex patterns are harder to manage and may cause unexpected files to appear.
Use '--cone' mode for simple folder-based sparse checkout.
Summary
Clone the repository with --no-checkout to avoid downloading all files.
Enable sparse checkout mode with 'git sparse-checkout init --cone'.
Specify which folders or files to include using 'git sparse-checkout set'.
Checkout the branch to populate the working directory with only the selected files.
Verify by listing files to confirm only the desired parts are present.