Sub topic 13
Partial Clones with --filter
This chapter assumes you can run git clone and know what commits, trees, and blobs are. A tree is a directory snapshot, and a blob is the contents of one file version. If those terms are new, review the chapters on what a clone creates and on shallow clones first.
What a partial clone is
A normal clone downloads every object in the repository's history. A partial clone downloads the full commit history but leaves out some objects, usually file contents. Git fetches the missing objects later, when a command needs them.
Shallow clones (--depth) cut off history. Partial clones keep all history and skip data instead. For that reason, commands like git log and git blame still work on a partial clone without extra downloads. The commit graph is complete.
Cloning without file contents
git clone --filter=blob:none https://github.com/octocat/Hello-World.gitThe filter blob:none tells the server to leave out all blobs. Git still downloads all commits and trees. It then checks out the default branch, which requires the blobs for that one snapshot, so it fetches those in a second request.
The server that supplied the clone becomes a promisor remote. This is a remote that promises to supply any missing object on request. Git records the setting in your repository's configuration:
cd Hello-World
git config remote.origin.promisor
git config remote.origin.partialclonefilterThe first command prints true. The second prints blob:none.
Other filters
| Filter | What it omits | Typical use |
|---|---|---|
| `blob:none` | All file contents | Large repositories you will work in normally |
| `blob:limit=1m` | Files larger than 1 MiB | Repositories with a few huge binary files |
| `tree:0` | All trees and blobs | CI jobs that only read commit metadata |
For example, this command skips only large files:
git clone --filter=blob:limit=1m https://github.com/octocat/Hello-World.gitGotchas
- On-demand fetches need the network. If you go offline, commands that need an omitted blob fail. Examples are checking out an old commit or running
git log -p. - Server support is required. GitHub supports filters. Some self-hosted servers do not, and Git then warns that the filter was ignored and downloads everything.
- Slow commands can surprise you. A command that touches many old files, such as
git blameon a large file, triggers many small fetches. Git batches some of these, but it can still be slower than a full clone. tree:0is aggressive. Evengit logwith path filters can trigger many fetches. Preferblob:noneunless you only need commit data.
Fetching everything later
If you decide you want all contents, remove the filter and fetch the missing objects:
git config --unset remote.origin.partialclonefilter
git fetch --refetch originThe --refetch option needs Git 2.36 or newer. On older versions, make a new full clone.
Exercise
A teammate says a --depth 1 clone and a --filter=blob:none clone are the same thing. Name one command that behaves differently between them and say why.
Show answer
git log shows the difference. In a --depth 1 clone it lists only one commit because the history was cut off. In a --filter=blob:none clone it lists the full history, because all commits are downloaded and only file contents are deferred.