Fetching a Single Folder from a Git Repository with Git Sparse-Checkout

If you’ve ever worked with large Git repositories—especially giant monorepos or model repositories with gigabytes of data—you’re probably familiar with the pain of waiting for Git to grind through thousands of files, or worse, downloading a mountain of data you don’t actually need. Wouldn’t it be nice if you could simply grab just the folder you want, skipping the rest? Thanks to Git’s sparse-checkout feature, you can!

In this guide, you’ll learn how to fetch a single folder (or a subset of folders) from a Git repository using sparse-checkout, explore practical scenarios where this shines, understand how it works under the hood, and see some advanced tricks to save even more time and disk space.


Quick Reference: The Minimal Sparse-Checkout Workflow

Here’s the “just show me how” section. If you know what you’re doing and just want the commands, here you go!

# 1. Clone the repo WITHOUT checking out any files yet git clone <REPO_URL> --no-checkout <directory> cd <directory> # 2. Enable sparse-checkout in "cone" mode (recommended) git sparse-checkout init --cone # 3. Specify the folder(s) you want to actually pull out git sparse-checkout set path/to/folder1 path/to/folder2 # 4. Checkout the desired branch (main by default) git checkout # or: git switch main

Optional: Clone shallowly (skip all but the latest commit and save bandwidth/storage):

This is great when a large repo will have a length commit history that might still take a while to download.

git clone <REPO_URL> --no-checkout <directory> --depth 1

Why Use Sparse-Checkout?

Common Scenarios

  1. Large Monorepos:
    Modern teams often split work into subdirectories in a single massive repository (“monorepo”). If you’re only touching one app or library out of dozens, you don’t need the rest on your local machine.

  2. Model/Asset Repositories:
    Platforms like HuggingFace host hundreds of GBs of models in one repo. Want to use only a single model? Download only the relevant folder, instead of everything!

  3. Legacy Repositories:
    Many open source projects have huge histories and lots of old code you won’t even see or care about. Why let that slow you down?

Major Benefits

  • Faster git status, git checkout, and all file-ops because Git has less to scan.

  • Reduced file clutter in your working directory.

  • Targeted development—see (and touch) only your portion of the project.


How Does Sparse-Checkout Work?

At its core, sparse-checkout tells Git to only populate your working directory with a subset of files from the repository. Behind the scenes, the .git/index and .git/info/sparse-checkout file keep track of what's included in your local checkout.

Key Points:

  • All Git objects (commits, trees, blobs) are downloaded by default. Only your working directory is sparse.

  • In “cone mode” (the default for modern Git), you specify whole directories. This is fast and less error-prone.

  • You can dynamically add or remove included folders with more commands (see below).

Note: Requires Git 2.25 or newer. Cone mode (recommended for almost everyone) was finalized and enabled by default in Git 2.27.


Step-by-Step: Fetching a Single Folder

Let’s walk through the process in more detail, clarifying what each command does.

1. Clone the Repo Without Checking Out Files

Most of us usually run:

git clone https://github.com/bigcorp/huge-monorepo.git

But that clones and checks out the entire working tree—potentially millions of files!
Instead, use:

git clone https://github.com/bigcorp/huge-monorepo.git --no-checkout my-local-folder cd my-local-folder

This downloads the repository history, but does NOT put any project files in your working directory yet.

Want to save even more time & disk?

Add --depth 1 to fetch only the latest commit:

git clone https://github.com/bigcorp/huge-monorepo.git --no-checkout my-local-folder --depth 1

Tip: This is called a “shallow clone.” You lose the complete history, but if you only want the latest state or a single version, it saves space/time.


2. Enable Sparse-Checkout in Cone Mode

“Cone mode” is optimized for directories and prevents many subtle mistakes:

git sparse-checkout init --cone

3. Select the Folder(s) to Check Out

Replace path/to/folder with the path(s) to the subproject or model you want (relative to the repo root).

git sparse-checkout set apps/my_app libs/my_lib

You can specify multiple paths, separated by a space.
If you're unsure of the path, ls or browse the repo on GitHub/etc.


4. Actually Checkout the Files

Now tell Git to populate your working directory with just those paths:

git checkout # or `git switch main` as appropriate

Git will only materialize the .git files and your selected folders.
Everything else stays invisible (and out of the way!).


Advanced Usage & Extra Commands

Add More Folders Later

Decided you need something else? Add it without losing your previous settings:

git sparse-checkout add another_folder/yet_another

View Which Paths Are Included

git sparse-checkout list

Go Back to Full Checkout

Want all files back? You can disable sparse mode:

git sparse-checkout disable

Deeper Dive: Partial Clones and Space-Saving Tricks

“Sparse-checkout” limits what's in your working directory—but by default, allrepository objects (including blobs for files you aren't using) are still downloaded!

For true minimal downloads, combine “sparse-checkout” with “partial clone”:

git clone --filter=blob:none --sparse https://github.com/bigcorp/huge-monorepo.git cd huge-monorepo git sparse-checkout set path/to/needed_dir
  • --filter=blob:none tells Git to skip downloading file blobs until you actually need them (fetch-on-demand).

  • --sparse automatically enables sparse-checkout for you too!

Now, not even the file contents are downloaded unless you explicitly request them.


Best Practices and Gotchas

  • Keep your Git up-to-date. Features, bugfixes, and performance improvements land regularly, and old Git versions may have partial or buggy sparse-checkout support.

  • Cone mode simplifies life. Try to stick with whole directories unless you have an advanced/Git-specific use case.

  • Be careful with merges and rebases: sometimes, files outside your sparse cone may appear as conflicts or in status lists. Use git sparse-checkout reapply if things get weird.

  • Sparse-checkout is working on your view, not the repository itself. Collaborators are unaffected by what you include/exclude in your local sparse-checkout setup.


Real-World Example: Grabbing a Single HuggingFace Model

Say you want to download just one model folder from HuggingFace’s ginormous transformers model zoo:

git clone https://huggingface.co/models --no-checkout models cd models git sparse-checkout init --cone git sparse-checkout set bert-base-uncased git checkout

Now you have only the bert-base-uncased folder in your working directory—nothing else!


Resources and Further Reading


Conclusion

Sparse-checkout is a game changer for working with massive Git repositories or when you only care about a tiny slice of a monolith. Paired with shallow or partial clones, it can slash waiting time, disk usage, and cognitive clutter.

Next time you need just a small part of a monstrous repo, reach for sparse-checkout and take control of what you download!

Start Building in JavaScript Today

Lilbots is the best platform for creating powerful JavaScript bots

  • Built in access to cutting edge AI models
  • Out of the box integration with hundreds of APIs
  • Collaborate with coding workspaces
  • Discover and fork thousands of bots built by the community

Learn Linux

Nginx

Git