How to git clone only a specific file or directory
Git does not directly support cloning only a specific file or directory from a repository. However, you can use --depth 1
to clone only one specific revision (as opposed to the entire history) and use --no-checkout
followed by git sparse-checkout set
to checkout not the entire file tree but only a specific file.
In the following example, we’ll checkout only the EMQX config files (in apps/emqx/etc
) from the entire emqx repository:
git clone --depth 1 --branch v5.0.8 --no-checkout https://github.com/emqx/emqx.git
cd emqx
git sparse-checkout set apps/emqx/etc
git checkout v5.0.8
After this command, the emqx
folder will only contain .git
and apps/emqx/etc
.
Note that you can call git sparse-checkout set
multiple times in order to checkout multiple distinct paths.
This example was adapted from this StackOverflow post.