How do I clone a subdirectory only of a Git repository?

In this example, I will checkout crm from react-admin/examples/crm folder in react-admin repository. The steps to do a sparse clone are as follows

mkdir admin-crm
cd admin-crm
git init
git remote add -f origin

This creates an empty repository with your remote, and fetches all objects but doesn’t check them out. Then do:

git config core.sparseCheckout true

Now you need to define which files/folders you want to actually check out. This is done by listing them in .git/info/sparse-checkout, eg:

echo "examples/crm" >> .git/info/sparse-checkout
# echo "another/sub/tree" >> .git/info/sparse-checkout

Last but not least, update your empty repo with the state from the remote:

git pull origin master

You will now have files “checked out” for examples/crm on your file system (with those paths still), and no other paths present.

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.