Some repositories are huge, and I only care about one folder in them. Git can give me just that folder in the working tree, with the rest of the project still in the history.
Start with a clone that only puts the top level files on disk, and that skips downloading file contents until Git needs them:
git clone --filter=blob:none --sparse https://github.com/owner/project.git
cd project
git sparse-checkout set docs tools/scripts
--sparse starts a sparse checkout with only the files at the root of the repo. --filter=blob:none is a partial clone: blobs (the file contents) are fetched when needed instead of all at once. Then git sparse-checkout set picks the directories you want, and Git fills them in.
Need one more folder later? git sparse-checkout add src/api adds it to the list. git sparse-checkout list shows what you have, and git sparse-checkout disable brings back the full working tree.
This uses cone mode, which the docs call the default. You give directories, not patterns, and the files in the root of the repo are always included.
Caveats. The man page still labels the command experimental and says its behaviour may change. And set also deletes ignored files in any directory that no longer has tracked files in it, so a local .env or build output in a folder you drop from the list can disappear. I’d check for those before I narrow the list.
Further reading: git-sparse-checkout documentation, git-clone documentation: –sparse and –filter