How to Clone Only a Specific Directory with Git

A quick reference for how to clone only a specific directory from a Git repository.

Introduction

I needed to clone only a specific directory from a large repository to my local machine.

It wasn't immediately obvious how to do this, so I'm leaving a note here for future reference.

Note: This article was translated from my original post.

How to Clone Only a Specific Directory with Git

Commands

You can clone only a specific directory using the following commands:

git clone --filter=blob:none --sparse https://github.com/user/repo.git
cd repo
git sparse-checkout set path/to/directory
  • Line 1: git clone --filter=blob:none --sparse https://...

This first line uses --filter=blob:none to start without downloading any actual file content, and --sparse to enable sparse-checkout.

Sparse-checkout is a Git feature that limits the working tree (the part that actually gets checked out) to only a subset of the repository ("sparse" means "thinly scattered"). At this point, only the files directly under the root directory are checked out.


  • Line 2: cd repo

This simply moves into the cloned repository directory (which at this stage only contains files from the root level).


  • Line 3: git sparse-checkout set path/to/directory

The set command specifies the directory path to check out, and sparse-checkout ensures only that directory gets checked out.


With these Git commands, you can clone only a specific directory from a repository.

Let's walk through an actual example.

Example

We'll clone only the modules/cloud-run-v2 directory from Google Cloud's Terraform modules collection.

First, run the clone command pointing to the repository root, with the --filter=blob:none --sparse flags:

git clone --filter=blob:none --sparse https://github.com/GoogleCloudPlatform/cloud-foundation-fabric.git


Next, move into the repository directory:

cd cloud-foundation-fabric

If we check the files at this point, we can see that only the root-level files have been downloaded:

$ ls -la
total 1632
drwxr-xr-x  23 bioerrorlog  staff     736  2 23 22:07 .
drwxr-xr-x  93 bioerrorlog  staff    2976  2 23 22:07 ..
-rw-r--r--   1 bioerrorlog  staff      53  2 23 22:07 .codespellrc
drwxr-xr-x  13 bioerrorlog  staff     416  2 23 22:07 .git
-rw-r--r--   1 bioerrorlog  staff      85  2 23 22:07 .gitattributes
-rw-r--r--   1 bioerrorlog  staff     556  2 23 22:07 .gitignore
-rw-r--r--   1 bioerrorlog  staff    4090  2 23 22:07 .pre-commit-config.yaml
-rw-r--r--   1 bioerrorlog  staff      78  2 23 22:07 .style.yapf
-rw-r--r--   1 bioerrorlog  staff     182  2 23 22:07 .tflint.hcl
-rw-r--r--   1 bioerrorlog  staff     560  2 23 22:07 .yamllint
lrwxr-xr-x   1 bioerrorlog  staff       9  2 23 22:07 AGENTS.md -> GEMINI.md
-rw-r--r--   1 bioerrorlog  staff  311444  2 23 22:07 CHANGELOG.0.md
-rw-r--r--   1 bioerrorlog  staff  340612  2 23 22:07 CHANGELOG.md
-rw-r--r--   1 bioerrorlog  staff   63422  2 23 22:07 CONTRIBUTING.md
-rw-r--r--   1 bioerrorlog  staff   10981  2 23 22:07 CURSED_KNOWLEDGE.md
-rw-r--r--   1 bioerrorlog  staff    7815  2 23 22:07 FABRIC-AND-CFT.md
-rw-r--r--   1 bioerrorlog  staff    3533  2 23 22:07 GEMINI.md
-rw-r--r--   1 bioerrorlog  staff   11357  2 23 22:07 LICENSE
-rw-r--r--   1 bioerrorlog  staff    6487  2 23 22:07 README.md
-rw-r--r--   1 bioerrorlog  staff    3568  2 23 22:07 REFERENCES.md
-rw-r--r--   1 bioerrorlog  staff    1110  2 23 22:07 default-versions.tf
-rw-r--r--   1 bioerrorlog  staff    1114  2 23 22:07 default-versions.tofu
-rw-r--r--   1 bioerrorlog  staff   24436  2 23 22:07 diagram.svg


Finally, specify the target directory path modules/cloud-run-v2 and run the sparse-checkout:

git sparse-checkout set modules/cloud-run-v2

This downloads the specified directory.

Let's verify:

$ ll
total 1632
drwxr-xr-x  24 bioerrorlog  staff     768  2 23 22:13 ./
drwxr-xr-x  93 bioerrorlog  staff    2976  2 23 22:07 ../
-rw-r--r--   1 bioerrorlog  staff      53  2 23 22:07 .codespellrc
drwxr-xr-x  13 bioerrorlog  staff     416  2 23 22:13 .git/
-rw-r--r--   1 bioerrorlog  staff      85  2 23 22:07 .gitattributes
-rw-r--r--   1 bioerrorlog  staff     556  2 23 22:07 .gitignore
-rw-r--r--   1 bioerrorlog  staff    4090  2 23 22:07 .pre-commit-config.yaml
-rw-r--r--   1 bioerrorlog  staff      78  2 23 22:07 .style.yapf
-rw-r--r--   1 bioerrorlog  staff     182  2 23 22:07 .tflint.hcl
-rw-r--r--   1 bioerrorlog  staff     560  2 23 22:07 .yamllint
lrwxr-xr-x   1 bioerrorlog  staff       9  2 23 22:07 AGENTS.md@ -> GEMINI.md
-rw-r--r--   1 bioerrorlog  staff  311444  2 23 22:07 CHANGELOG.0.md
-rw-r--r--   1 bioerrorlog  staff  340612  2 23 22:07 CHANGELOG.md
-rw-r--r--   1 bioerrorlog  staff   63422  2 23 22:07 CONTRIBUTING.md
-rw-r--r--   1 bioerrorlog  staff   10981  2 23 22:07 CURSED_KNOWLEDGE.md
-rw-r--r--   1 bioerrorlog  staff    7815  2 23 22:07 FABRIC-AND-CFT.md
-rw-r--r--   1 bioerrorlog  staff    3533  2 23 22:07 GEMINI.md
-rw-r--r--   1 bioerrorlog  staff   11357  2 23 22:07 LICENSE
-rw-r--r--   1 bioerrorlog  staff    6487  2 23 22:07 README.md
-rw-r--r--   1 bioerrorlog  staff    3568  2 23 22:07 REFERENCES.md
-rw-r--r--   1 bioerrorlog  staff    1110  2 23 22:07 default-versions.tf
-rw-r--r--   1 bioerrorlog  staff    1114  2 23 22:07 default-versions.tofu
-rw-r--r--   1 bioerrorlog  staff   24436  2 23 22:07 diagram.svg
drwxr-xr-x   4 bioerrorlog  staff     128  2 23 22:13 modules/

# The modules directory has been downloaded

$ ls modules/             
README.md   cloud-run-v2

# Inside the modules directory, only the specified cloud-run-v2 (plus files directly under the directory) has been downloaded

Conclusion

That was a quick reference on how to clone only a specific directory with Git.

That's all!

[Related Articles]

en.bioerrorlog.work

References