DEV Community

loading...

Quick tip: Clone a single folder from Github

Copple
Cofounder of Supabase (supabase.io).
・1 min read

Sometimes you find a repository on Github and you only really need a single folder.

How do you get it without cloning the entire repo?

Luckily Github just implemented sparse-checkout. This is a super-quick example of how it works.

Example repo

Let's say I wanted to clone Supabase's example Slack clone, which lives in the following folder:

├── connectors
├── examples
│   ├── slack-clone-basic # <-- github.com/supabase/supabase/examples/slack-clone-basic
├── libraries
├── web
└── README.md

Sparse Checkout

Here are all the steps I would need:

# 1. Copy an empty repo
git clone --no-checkout https://github.com/supabase/supabase

# 2. Move into the empty repo
cd supabase

# 3. Initialize sparse-checkout
git sparse-checkout init --cone

#4. Checkout the folder
git sparse-checkout set examples/slack-clone-basic

And viola! Now you have just the folder you want.

Troubleshooting

sparse-checkout was introduced in Git version 2.25. If you get see an error "git: 'sparse-checkout' is not a git command" then you probably need to update. You can use git --version to see what version you're on.

If you're on Mac (and used homebrew to install git) you can run brew upgrade git.

Discussion (2)

Collapse
cescquintero profile image
Francisco Quintero 🇨🇴

Juuumm, interesting. Thanks for sharing!

Collapse
davidrbailey profile image
David Bailey