How to Clone Only Specific Directories with Git Sparse Checkout
In this tutorial, you'll learn about How to Clone Only Specific Directories with Git Sparse Checkout. We cover key concepts, practical examples, and best practices.
The Problem
You clone a large monorepo (tens of thousands of files, multi-GB history) but only need one or two directories. A full clone takes minutes and consumes gigabytes of disk space. Git sparse checkout lets you clone part of a repository by configuring which paths to include in your working tree. Even with sparse checkout, the full history is still downloaded unless you also use --filter=blob:none (partial clone) to skip file contents.
Quick Fix
1. Enable sparse checkout during clone (Git 2.25+)
git clone --filter=blob:none --sparse https://github.com/user/monorepo.git
cd monorepo
The --filter=blob:none flag skips downloading file contents during clone. The --sparse flag enables sparse checkout mode.
2. Specify which directories to include
git sparse-checkout set src/services docs/api
After this, only src/services and docs/api are present in your working directory. All other files exist in the git history but are not checked out.
3. List the current sparse checkout patterns
git sparse-checkout list
Expected output:
src/services
docs/api
4. Add directories to the sparse checkout
git sparse-checkout add tests/integration
This adds another directory without removing existing ones. Use this when you discover you need more files.
5. Full clone with sparse checkout (older Git versions)
git clone --no-checkout https://github.com/user/monorepo.git
cd monorepo
git sparse-checkout init --cone
git sparse-checkout set src/services
git checkout main
The --cone mode enables cone-mode sparse checkout, which is simpler and faster than the older full-pattern mode.
6. Recheck specific files or directories
git sparse-checkout reapply
If files are missing or the working tree is inconsistent, this reapplies the current sparse checkout rules.
7. Disable sparse checkout to restore all files
git sparse-checkout disable
This checks out all files in the repository. Use this when you need the full working tree temporarily.
8. Check disk space saved
du -sh .git
Expected output:
142M .git
Without sparse checkout and blob filtering, the .git directory might be 2GB+ for a large monorepo. The working directory size difference depends on how many directories you include.
9. Show disk usage of the working tree vs full repository
du -sh .git
du -sh --exclude=.git .
Compare the two numbers to see how much disk space sparse checkout is saving. For a large monorepo, the .git directory may be 2GB+ while the working tree is only 50MB.
Prevention
- Use
--filter=blob:nonewith--sparsefor maximum clone speed - Use cone mode (
--cone) which automatically includes parent directories - Add patterns with
git sparse-checkout add, not by editing.git/info/sparse-checkoutdirectly - Run
git sparse-checkout reapplyif the working tree gets out of sync - Document which sparse checkout patterns your team uses for each project
- Remember:
git addstill works within the checked-out directories — only the working tree is limited, not the index or staging
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro