patterngitTip
Sparse checkout: clone only specific directories of a large monorepo
Viewed 0 times
sparse checkoutpartial clonemonorepofilter blobcone mode
Problem
Cloning a large monorepo takes many minutes and gigabytes of disk space when you only need to work on one service or package.
Solution
Use sparse checkout combined with a partial clone to fetch only the files you need:
# Clone without checking out any files, blob-less for speed
git clone --filter=blob:none --no-checkout https://github.com/org/monorepo.git
cd monorepo
# Enable sparse checkout
git sparse-checkout init --cone
# Specify which directories to check out
git sparse-checkout set services/payments packages/shared
# Now checkout
git checkout main
# Clone without checking out any files, blob-less for speed
git clone --filter=blob:none --no-checkout https://github.com/org/monorepo.git
cd monorepo
# Enable sparse checkout
git sparse-checkout init --cone
# Specify which directories to check out
git sparse-checkout set services/payments packages/shared
# Now checkout
git checkout main
Why
Sparse checkout keeps the full tree in the Git index but only materializes specified paths in the working tree. Combined with
--filter=blob:none, blobs are fetched lazily on demand.Gotchas
- Cone mode (--cone) is faster and simpler but only supports full directory patterns, not arbitrary globs
- Without --cone, pattern matching is slow on large repos
- Some Git operations (grep, log --all) still operate on all objects even with sparse checkout
Code Snippets
Setting up sparse checkout for a monorepo
# Partial clone + sparse checkout
git clone --filter=blob:none --no-checkout https://github.com/org/big-monorepo.git
cd big-monorepo
git sparse-checkout init --cone
git sparse-checkout set apps/my-service packages/utils
git checkout main
# Add more directories later
git sparse-checkout add apps/another-service
# See what's currently included
git sparse-checkout listContext
Working in large monorepos where you only need a subset of the codebase
Revisions (0)
No revisions yet.