How do I clone a subdirectory only of a Git repository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When dealing with large Git repositories, sometimes you might only need a specific subdirectory from the repo. Cloning the entire repository just to access one subdirectory can be inefficient and time-consuming, especially if you're bandwidth-constrained or storage-limited. Fortunately, there are ways to clone just a subdirectory from a Git repository through a technique known as "sparse checkout." Here's how you can achieve that.
Sparse Checkout
Sparse checkout allows you to clone only part of a repository. This approach is built into Git and can be used quite effectively for this purpose.
Step-by-Step Guide to Sparse Checkout
Here's how you can clone just a subdirectory from a Git repository using sparse checkout:
- Initialize the Repository: First, create a new directory for your cloned repository and initialize it.
- Set the Remote URL: Set the URL of the remote repository you want to clone.
Replace <remote_url> with the URL of the repository.
- Enable Sparse Checkout: Enable sparse checkout in your new directory by configuring Git.
- Define the Subdirectory: Specify the path to the subdirectory you wish to clone. This requires modifying the
.git/info/sparse-checkoutfile.
Ensure that you replace path/to/subdirectory/ with the actual path of the subdirectory you need.
- Pull the Content: Finally, pull the contents from the remote repository.
Replace main with the appropriate branch if it differs.
Example Usage
Imagine a remote repository at https://github.com/user/repo.git and you want to clone just the docs subdirectory. Follow the steps outlined:
After executing these commands, your docs-repo directory will only contain files from the docs subdirectory of the remote repository.
Other Considerations
Supported Git Version
Sparse checkout is supported in modern versions of Git. Ensure that your Git version is at least 1.7 and recommend using a more recent version for improvements and bug fixes. You can check your Git version using:
Limitations
- No History Cloning: Sparse checkout doesn't clone the full history of a subdirectory; it just checks out the current version.
- Possible Complications with Deeply Nested Repos: If the repository structure is highly nested, defining sparse checkout rules may become complex.
Advanced Topic: Use of Partial Clone
Introduced in Git 2.19, partial clone is a more advanced feature that can be combined with sparse checkout when cloning large repositories over bandwidth-constrained networks. However, configuring a partial clone is more intricate and often requires support on both the client and server sides.
Summary Table
| Feature | Description |
| Sparse Checkout | Clones only selected directories from the repo |
| Setup Complexity | Straightforward with simple config tweaks |
| History | No historical data retrieved, only current content |
| Minimum Git Version | 1.7+ (recommended recent) |
| Optional Enhanced Tool | Partial clone with git 2.19+ for more bandwidth control |
Sparse checkout is an efficient method to fetch only what's necessary from large Git repositories, saving time and resources. Whether you're managing large-scale projects, dealing with limited bandwidth, or simply organizing your workflows more efficiently, sparse checkout can be an invaluable tool.

