How to Clone Only a Subdirectory of a Git Repository?
Last Updated :
03 Jun, 2024
In some scenarios, you may need to work with only a specific subdirectory of a large Git repository. Unfortunately, Git does not support cloning a subdirectory directly. However, there are a few effective workarounds to achieve this, including using sparse checkout or exporting the subdirectory. This guide will walk you through these methods.
Approach 1: Using Sparse Checkout
Sparse checkout allows you to check out only part of the working directory. This is particularly useful for large repositories where you only need a specific subdirectory.
Step 1: Initialize the Repository
First, clone the repository with the --no-checkout option to avoid checking out the files immediately.
git clone --no-checkout <repository-url>
cd <repository-directory>
Step 2: Enable Sparse Checkout
Configure Git to enable sparse checkout.
git sparse-checkout init
Step 3: Define the Subdirectory
Specify the subdirectory you want to clone. For example, if you want to clone the docs subdirectory:
git sparse-checkout set docs
Step 4: Checkout the Subdirectory
Now, checkout the repository. Only the specified subdirectory will be checked out.
git checkout main
Replace main with the appropriate branch name if it differs.
How to Clone Only a Subdirectory of a Git Repository?Approach 2: Using Git Archive
The git archive command can create an archive of a specific subdirectory. This method doesn't require cloning the entire repository.
Step 1: Create an Archive
Run the following command to create a tar archive of the desired subdirectory. Replace <repository-url> with your repository URL and <subdirectory> with the path to the subdirectory.
git archive --remote=<repository-url> HEAD:<subdirectory> | tar -x
For example, to archive the docs subdirectory:
git archive --remote=https://github.com/user/repo.git HEAD:docs | tar -x
This will create a local copy of the docs subdirectory.
Approach 3: Using Partial Clone (Git 2.19+)
Partial clone allows you to fetch only necessary objects. While not as precise as sparse checkout, it reduces the amount of data transferred.
Step 1: Clone the Repository with Partial Clone
Use the --filter option to exclude large blobs. This doesn't directly target subdirectories but can help if your goal is to minimize the download size.
git clone --filter=blob:none <repository-url>
cd <repository-directory>
Enable and set sparse checkout as shown in Method 1 to get only the desired subdirectory.
git sparse-checkout init
git sparse-checkout set <subdirectory>
git checkout main
Conclusion
While Git does not provide a direct way to clone only a subdirectory, the methods outlined above offer effective workarounds. Sparse checkout is the most flexible and widely applicable method, allowing you to selectively check out parts of a repository. Using git archive is a simple approach for quickly extracting a subdirectory without cloning the entire repository. Partial clone, combined with sparse checkout, is useful for large repositories with many large files. Choose the method that best fits your needs and work efficiently with specific subdirectories in large repositories.
Similar Reads
How to Git Clone a Local Repository? Git is a powerful tool for version control, and one of its most used commands is git clone. While it's commonly used to copy remote repositories from platforms like GitHub or GitLab, you can also clone a local repository right on your computer.PrerequisitesBefore you begin, ensure you have the follo
3 min read
How To Move Subdirectory Into Separate Git Repository? When working with large monolithic Git repositories, teams often find that separating parts of the codebase into distinct repositories can improve maintainability and focus. This article explains How To Move Subdirectory Into Separate Git Repository while preserving its history. Steps To Move Subdir
3 min read
How To Clone a Git Repository Into a Specific Folder? Cloning a Git repository is a fundamental task for developers, allowing you to create a local copy of a remote repository. While the default behavior places the cloned repository into a directory named after the repository itself, there are instances where you might want to specify a different direc
3 min read
How to Git Clone a Remote Repository? Git is a powerful version control system that allows developers to track changes, collaborate on code, and manage projects efficiently. One of the fundamental operations in Git is cloning a remote repository. This article will guide you through the process of cloning a remote Git repository. Prerequ
3 min read
How to Clone Git Repositories Including Submodules? Git submodules are repositories embedded inside another repository. They allow you to incorporate external projects into your repository at a specific commit, giving you precise control over their versions. Cloning a repository with submodules requires a few extra steps to ensure the submodules are
4 min read
How To Clone a Repository From Gitlab? Cloning a repository in Git involves creating a local copy of a project from a remote server. This allows you to work on the project on your local machine and later push your changes back to the remote repository. GitLab, one of the popular platforms for hosting Git repositories, provides a straight
3 min read