Git Sparse Checkout: Clone Only Part of a Repository
Use git sparse-checkout to clone and work with only specific directories of a large repository. Save disk space and improve performance.
git sparse-checkout set src/components src/libDetailed Explanation
What Is Sparse Checkout?
Sparse checkout lets you check out only a subset of files from a repository. Instead of populating your working directory with every file, you specify which directories or files you need. This is invaluable for monorepos and very large repositories.
Setting Up Sparse Checkout
# Clone without checking out files
git clone --no-checkout https://github.com/org/monorepo.git
cd monorepo
# Enable sparse checkout
git sparse-checkout init --cone
# Specify directories to include
git sparse-checkout set src/components src/lib
# Checkout
git checkout main
Cone Mode vs. Non-Cone Mode
- Cone mode (recommended,
--cone): Patterns match entire directories. Fast and efficient. - Non-cone mode: Supports arbitrary gitignore-style patterns. Slower but more flexible.
Managing Sparse Checkout
# View current patterns
git sparse-checkout list
# Add more directories
git sparse-checkout add src/utils
# Disable sparse checkout (restore all files)
git sparse-checkout disable
Combining with Partial Clone
For maximum efficiency, combine sparse checkout with a partial clone to avoid downloading blobs you do not need:
git clone --filter=blob:none --sparse https://github.com/org/monorepo.git
cd monorepo
git sparse-checkout set packages/my-package
This downloads only the Git metadata initially. Blobs are fetched on demand as you check out files.
When to Use Sparse Checkout
- Monorepos where you only work on one package.
- Large repos with media assets you do not need locally.
- CI/CD pipelines that only build or test a specific subdirectory.
Limitations
- Some Git operations (e.g.,
git stash) may behave unexpectedly with sparse checkout. - Team members must each configure their own sparse checkout patterns.
- Not all Git GUIs fully support sparse checkout.
Sparse checkout turns an unwieldy monorepo into a focused, fast workspace.
Use Case
A developer working on a monorepo with 50 packages only needs the frontend package. They use sparse checkout to check out only that package, reducing clone time and disk usage.