brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git sparse-checkout set gives you only the folders you need from a big repo

Git
~/tips/tip-git-sparse-checkout-set

git sparse-checkout set docs tools/scripts

Checks out only chosen folders of a large Git repository, so the working tree stays small.

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

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link