I took this from TypeScript definitions library @types:
Let's say the repo has this structure:
types/
|_ identity/
|_ etc...
Your goal: Checkout identity/ folder ONLY. With all its contents including subfolders.
⚠️ This requires minimum git version 2.27.0, which is likely newer than the default on most machines. More complicated procedures are available in older versions, but not covered by this guide.
git clone --sparse --filter=blob:none --depth=1 <source-repo-url>
git sparse-checkout add types/identity types/identity ...
This will check out the types/identity folder to your local machine.
--sparse
initializes the sparse-checkout file so the working directory starts with only the files in the root of the repository.
--filter=blob:none
will exclude files, fetching them only as needed.
--depth=1
will further improve clone speed by truncating commit history, but it may cause issues as summarized here.