Partial clones and sparse checkout

Advanced · 10 min

Work in a huge repository without downloading all of it: blobless partial clones, sparse checkout of one folder, shallow clones, and the trade-offs of each.

Before thisFetch, pull and pushPackfiles, garbage collection and repo size

Bryant Son · 10 min

Partial clones and sparse checkout

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Bryant SonReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A partial clone, `git clone --filter=blob:none`, downloads commits and trees now and fetches file contents (blobs) lazily when a command needs them.
  • Sparse checkout limits which folders are written into your working tree, and `git clone --sparse` plus `git sparse-checkout set <dir>` starts you with only the root files and the folders you choose.
  • The two combine: a partial clone controls what is downloaded and sparse checkout controls what is checked out, so a monorepo clone can stay tiny.
  • A shallow clone, `--depth N`, truncates history instead, which is faster but limits `log`, `blame` and merges until you `git fetch --unshallow`.
  • The server must allow filtering (`uploadpack.allowFilter`), and a partial clone needs network access later for any blob it didn't fetch.

When a repository is too big to want

A monorepo with ten years of history and gigabytes of assets takes a long time to clone, and most of it is irrelevant to you. You work on the CLI. There are two different costs to attack: what you download, and what ends up in your folder. Git has a separate tool for each.

I tested on a small stand-in monorepo: apps/cli, apps/web, docs, a 2 MB assets/video.b64, and five commits, one of which tweaked the big file. The remote was a bare repository served with file://, with git config uploadpack.allowFilter true set on it, which the server must allow. Git 2.54. Numbers are from my machine and will differ slightly.

(Use file:// rather than a plain path. A plain-path clone ignores filters, and file:// goes through the real transfer code, like a network remote would.)

Partial clone: skip the blobs for now

$ git clone --filter=blob:none --sparse file:///.../hub.git part
Cloning into 'part'...
$ du -sh part/.git
144K

Compare with a normal clone of the same repository: 1.6 MB in .git and 3.5 MB total. --filter=blob:none tells the server “send me the commits and trees, but no file contents”. Git remembers that this remote is a promisor:

$ git config --get-regexp '^remote.origin.(promisor|partialclonefilter)'
remote.origin.promisor true
remote.origin.partialclonefilter blob:none

Missing blobs are fetched the first time something needs them. I made a second blobless clone without checking anything out, and counted what was missing:

$ git clone -q --filter=blob:none --no-checkout file:///.../hub.git p2
$ git rev-list --objects --all --missing=print | grep -c '^?'
9
$ du -sh .git
120K
$ git show HEAD:assets/video.b64 | wc -c
 2000003
$ git rev-list --objects --all --missing=print | grep -c '^?'
8
$ du -sh .git
1.6M

Nine blobs missing. git show of the big file triggered a fetch of just that blob (the count dropped to 8, .git grew to 1.6 MB). Other filters exist: --filter=blob:limit=100k skips only large blobs, and tree:0 skips trees too. Because history operations like git log -p or blame fetch blobs one by one, they can be slow on a partial clone, and they need the network.

Sparse checkout: only some folders on disk

--sparse started my clone with only the root-level files:

$ cd part && ls
README.md
$ git sparse-checkout set apps/cli
$ ls; ls apps
README.md  apps
cli
$ git sparse-checkout add docs
$ git sparse-checkout list
apps/cli
docs

set replaces the list of folders, and add appends. These use “cone mode”, where you name directories and get everything under them. Because this was a blobless clone, set apps/cli made Git fetch only the blobs for apps/cli, and .git stayed small. apps/web and assets are in the commit but absent from my disk and my downloads.

Finally I turned it off:

$ git sparse-checkout disable
$ ls
README.md  apps  assets  docs
$ du -sh .git
1.6M

Disabling checked everything out, so Git had to fetch the 2 MB asset: .git jumped back to 1.6 MB. That is the lazy download paying off, and collecting its bill.

Shallow clones: cut history instead

$ git clone -q --depth 1 file:///.../hub.git sh
$ git log --oneline
ec8791b tweak asset
$ git rev-parse --is-shallow-repository
true
$ git fetch --unshallow
$ git log --oneline | wc -l
5

A depth-1 clone has one commit and no earlier history. It is the fastest way to get a build, and it’s the usual choice in CI. But blame, log and merge-base searches need history. --unshallow completes it.

Choosing

Problem Tool
Too many files in my folder sparse checkout
Too much data downloaded partial clone (--filter)
Both, in a monorepo --filter=blob:none --sparse
Only need the latest code (CI) --depth 1
Big binaries committed Git LFS, or keep them out

Git also ships a scalar command that sets up a partial clone with sparse checkout and background maintenance. I didn’t run it.

What the video covers

Bryant Son’s “Optimize checkout and clone time for GitHub monorepos using sparse-checkout and filter commands” is about ten minutes and was published around 2022. From its title it covers exactly this lesson’s two tools, sparse-checkout and the --filter option, aimed at large monorepos. I judge English from the title, description and channel only, and I could not read a transcript. I don’t know whether it covers shallow clones, so that section, the file:// tip and the numbers above are from my own runs. It replaced an older (about 2020) GitLab video that also spent time on file locking; that one and a newer Modern SDE lesson are linked as alternatives.

Where this fits

You can now keep large repositories manageable. The last lesson in this group is about trust: signing commits and tags.

Check yourself

  1. 01What does `git clone --filter=blob:none` skip, and when does it get it?

    It skips file contents (blobs) for every commit. Git downloads the ones it needs on demand, such as at checkout or when you run `git show` on a file.

  2. 02How is sparse checkout different from a partial clone?

    Sparse checkout limits which files appear in your working folder. A partial clone limits which objects are downloaded into `.git`. They solve different halves of the size problem.

  3. 03What do you lose with a `--depth 1` shallow clone?

    Most history, so `git log`, `git blame` and finding merge bases are limited. `git fetch --unshallow` restores the rest.

  4. 04You disabled sparse checkout in a blobless clone and `.git` grew. Why?

    Checking out every file requires every blob in the current commit, so Git had to fetch the ones it had skipped.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark