Large Repositories: Sparse Checkout, Partial Clone, Maintenance and Scalar
Keep Git fast in big repositories and monorepos: sparse checkout in cone mode, partial clones that fetch files on demand, git backfill, background maintenance with commit-graphs and geometric repacking, the file system monitor and untracked cache, and Scalar, which sets it all up.
kvi5hnuCode Sprout team member7 min readOn this page
Git handles most repositories without any tuning. But in a large monorepo, with millions of files, hundreds of thousands of commits or gigabytes of history, a plain clone can take an hour and git status several seconds. Git has a set of features for exactly this, used by some of the largest repositories in the world. This article covers them, from "only check out the folders I work on" to background maintenance.
| Problem | Feature |
|---|---|
| too many files in the working tree | sparse checkout |
| clone downloads too much history | partial clone (or shallow clone) |
| history commands slow | commit-graph, maintenance |
git status slow | fsmonitor, untracked cache, sparse index |
| all of it, configured at once | Scalar |
Sparse checkout: only the folders you need
Sparse checkout keeps the whole repository, but writes only chosen folders to disk. In cone mode, the default, you list folders, and Git includes them plus the files at the top level:
git init -q monorepo && cd monorepo
mkdir -p web/src api/src docs tools
for d in web/src api/src docs tools; do echo "code" > $d/main.txt; done
echo "# Monorepo" > README.md
git add .; git commit -q -m "Initial layout"
for i in 1 2 3; do echo "change $i" >> api/src/main.txt; git commit -q -am "API change $i"; done
git config uploadpack.allowFilter true # let clones ask for a filter
cd ..
git clone -q --filter=blob:none --sparse "file://$PWD/monorepo" work
cd work
echo "=== after cloning: only top-level files"
ls
git sparse-checkout set web docs
echo "=== after 'set web docs'"
ls
git sparse-checkout add api
git sparse-checkout list
echo "=== file versions not downloaded yet: $(git rev-list --objects --all --missing=print | grep -c '^?')"
git backfill
echo "=== after git backfill: $(git rev-list --objects --all --missing=print | grep -c '^?')"
=== after cloning: only top-level files
README.md
=== after 'set web docs'
README.md
docs
web
api
docs
web
=== file versions not downloaded yet: 2
=== after git backfill: 0
git clone --sparsestarts with only the top-level files. In an existing clone,git sparse-checkout set <folders>turns sparse checkout on.setreplaces the list,addextends it, andlistshows it.disablereturns to a full checkout.reapplyre-applies the rules after something put files outside them.- Commands like
git status,git addandgit switchstay fast, because Git ignores the rest of the tree. History, branches and commits are unaffected: a commit still records the whole tree. git sparse-checkout clean(Git 2.52) removes leftover files outside the chosen folders, such as untracked build outputs, removing whole folders outside the cone. It refuses to run without an option: preview with--dry-run, then run it with-f.
For the biggest repositories, also enable the sparse index, git sparse-checkout init --cone --sparse-index or index.sparse=true, which keeps the index itself small.
Non-cone mode, --no-cone, accepts .gitignore-style patterns instead of folders. It's slower and harder to reason about, and kept mainly for compatibility.
Partial clone: download content on demand
A partial clone downloads all commits but leaves out some objects, which Git fetches from the server when they're needed (article 18):
| Filter | Leaves out | Good for |
|---|---|---|
--filter=blob:none | all file contents not needed for the checkout | everyday development: history works, old file versions come on demand |
--filter=blob:limit=1m | files over 1 MB | repositories with some large binaries |
--filter=tree:0 | trees and blobs not needed for the checkout | CI and single-build checkouts. Slow for history commands |
The server must allow filters, as with uploadpack.allowFilter above. GitHub, GitLab and other major hosts do. Commands that need old content, such as git log -p or git blame, fetch it automatically, which can be slow when it's one file at a time. git backfill (Git 2.49) downloads missing content in efficient batches, and since Git 2.54 accepts revisions and paths to limit what it fetches, such as git backfill -- api/.
Combined with sparse checkout, as above, a partial clone downloads only what your folders need.
Shallow clones (--depth 1) cut off history instead. They suit throwaway CI builds, but make log, blame and merges with older history unreliable. For a working copy, prefer a blob-less partial clone.
Maintenance: keeping things fast over time
As commits accumulate, Git's data needs reorganizing: loose objects packed, packs combined, and indexes for fast history walks updated. git maintenance schedules this in the background:
git maintenance start # register this repository and schedule hourly/daily/weekly tasks
git maintenance stop # unschedule them
git maintenance run --task=commit-graph --task=prefetch # run tasks now
start uses the system scheduler: launchd on macOS, Task Scheduler on Windows, and systemd timers or cron on Linux. The main tasks:
prefetchfetches from remotes in the background into a hidden area, so yourgit fetchhas little left to download. It doesn't change your remote-tracking branches.commit-graphmaintains the commit-graph file, which makeslog --graph,merge-base, reachability checks and branch counts much faster.loose-objectsandincremental-repackpack objects without rewriting everything.gcis the traditional full garbage collection.
maintenance.strategy picks a set of tasks and schedules. Since Git 2.54, the default is geometric (added in Git 2.52), which repacks so that pack sizes grow geometrically, avoiding expensive rewrites of everything at once.
git init -q trail-guide && cd trail-guide
for i in $(seq 1 30); do git commit -q --allow-empty -m "Commit $i"; done
echo "loose objects before: $(git count-objects | cut -d' ' -f1)"
git maintenance run --task=gc --task=commit-graph --quiet
echo "loose objects after: $(git count-objects | cut -d' ' -f1)"
ls .git/objects/info/
git commit-graph verify && echo "commit-graph OK"
loose objects before: 31
loose objects after: 0
commit-graph
packs
commit-graph OK
Even without scheduled maintenance, commands such as commit, merge and fetch run git maintenance run --auto, which with the default geometric strategy repacks once there are many loose objects. Setting fetch.writeCommitGraph=true also updates the commit-graph after each fetch. Scalar does the opposite: it turns automatic maintenance off and leaves everything to the scheduled tasks.
Making git status fast
git status compares every file in the working tree with the index. Two settings remove most of that work:
[core]
fsmonitor = true # a built-in file system watcher reports changed files
untrackedCache = true # remember which folders had no untracked files
[feature]
manyFiles = true # enables index version 4 and the untracked cache, among others
With core.fsmonitor=true, Git starts a background daemon (git fsmonitor--daemon) that watches the folder, so status only checks what changed. It's available on Windows and macOS, and on Linux since Git 2.55. With older versions on Linux, a hook-based integration with Watchman can be used instead.
Scalar
Scalar, installed with Git, applies all the recommended settings for large repositories in one command:
scalar clone https://github.com/acme/monorepo.git # partial, sparse clone + maintenance + settings
scalar register # apply the same to an existing clone
scalar list # repositories Scalar manages
scalar unregister
scalar clone creates a blob-less partial clone with sparse checkout in cone mode (starting with the top-level folder only), and places the working tree in a src subfolder. scalar register enables fsmonitor, the commit-graph and multi-pack index settings, and scheduled maintenance. Since Git 2.50, --no-maintenance skips scheduling it.
What's current
git backfill(Git 2.49, extended in 2.54 and 2.55) bulk-downloads content missing from blob-less clones.git sparse-checkout clean(Git 2.52) removes leftover files outside the sparse cone.git maintenancegained the geometric strategy in Git 2.52, and uses it by default since Git 2.54.- Scalar ships with Git, and is the quickest way to configure a large repository.
Common questions
Sparse checkout or partial clone?
They solve different problems and work well together. Sparse checkout limits which files are written to disk. Partial clone limits what's downloaded. Use both for a large monorepo: git clone --filter=blob:none --sparse <url>, then git sparse-checkout set <folders>.
Can I commit in a sparse checkout? Yes. Commits record the whole tree, and the folders you don't have checked out are carried over unchanged from the parent commit.
Why is git log -p slow in my blob-less clone?
It needs old versions of files, which are fetched one by one on demand. Run git backfill, or git backfill -- <path> for the parts you care about, to download them in batches first.
Does git maintenance start slow my computer down?
Its tasks are incremental and run in the background at low frequency, typically hourly prefetch and commit-graph updates, plus daily and weekly packing. On small repositories, they finish in moments.
Summary
- Sparse checkout (
git sparse-checkout set <folders>, cone mode) writes only chosen folders to disk, andclean(Git 2.52) removes leftovers. - Partial clones (
--filter=blob:none) fetch file contents on demand.git backfillfetches them in batches. git maintenance startschedules prefetch, commit-graph and packing tasks. The geometric strategy is the default since Git 2.54.core.fsmonitor,core.untrackedCacheandfeature.manyFilesmakegit statusfast.scalar cloneandscalar registerapply all of this in one step.
Practice questions
You only work in services/payments/ of a huge monorepo. How do you clone it efficiently?
Answer
git clone --filter=blob:none --sparse https://github.com/acme/monorepo.git
cd monorepo
git sparse-checkout set services/payments
The partial clone skips file contents outside what's checked out, and sparse checkout writes only that folder plus top-level files. scalar clone does the same with extra performance settings.
What's the difference between --filter=blob:none and --depth 1?
Answer
--filter=blob:none downloads every commit and tree, so history, log, merge-base and branching all work, and fetches file contents only when needed. --depth 1 downloads only the latest commit, cutting off history, which breaks or limits log, blame and merges with older commits. Partial clones suit development, and shallow clones suit one-off builds.
git status takes four seconds in a repository with 2 million files on macOS. What do you try first?
Answer
Enable the file system monitor and untracked cache: git config core.fsmonitor true and git config core.untrackedCache true, or feature.manyFiles=true. If you only need part of the tree, add sparse checkout. scalar register applies these settings and scheduled maintenance at once.
Review quiz
Test yourself on Part 9 with the Git Part 9 review quiz.
Previous: Large Files: Git LFS and Keeping Binaries Out of History

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.