DEV Community

Copple
Copple

Posted on

Quick tip: Clone a single folder from Github

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.

Top comments (4)

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

Juuumm, interesting. Thanks for sharing!

Collapse
 
aleclarson profile image
Alec Larson

This helped me when upgrading git:
apple.stackexchange.com/a/285131/7...

Collapse
 
davidrbailey profile image
David Bailey
Collapse
 
temesgenchali profile image
Temesgen

interested thank you