Sub topic 13
Choosing a strategy for development, review, and inspection
This chapter assumes you can clone, fetch, branch, and commit. It also assumes you know that Git stores commits, trees, and blobs, and that you have seen the options --depth, --filter, and sparse checkout in the earlier chapters. Here you will use them to pick a clone for a specific job.
The decision in one question
No clone strategy is best in general. Each one removes something from a full clone, and the right choice depends on what your task needs to read. Ask three things:
- Do I need history (log, blame, merge base), or only the current files?
- Do I need the whole working tree, or only part of it?
- Will I be online while I work, and for how long will the clone live?
The answers map to the tools you already know.
| Tool | What it removes | What still works | Main cost |
|---|---|---|---|
| `--single-branch --no-tags` | Other branches and tags | Full history of one branch | Other refs must be fetched later |
| `--depth N` | Commits older than N | Current files, recent log | History commands are wrong or limited |
| `--filter=blob:none` | File contents until needed | Full log, merge-base, branching | Network fetches when contents are read |
| `--filter=tree:0` | Trees and blobs until needed | Commit graph | Many small fetches if you browse history |
| `--sparse` | Files outside chosen paths in the working tree | Everything inside the chosen paths | You must choose paths |
Depth limits commits. Filters limit objects. Sparse checkout limits the working tree. These three controls are independent, so you can combine them.
Profile 1: daily development
A developer commits, branches, rebases, reads history, and runs git blame. These commands need the full commit graph, and they often need older file versions.
A shallow clone is a poor fit here. It truncates history, so git blame stops at the shallow boundary, git merge-base can fail or give wrong answers, and pushing from a shallow repository can be rejected by some servers. You would keep running git fetch --deepen and eventually --unshallow.
A blobless clone is the usual answer:
git clone --filter=blob:none https://example.com/org/big-repo.git
cd big-repo
git log --oneline -- README.md
git show HEAD:README.mdYou get every commit and tree, so log, branch, and merge-base work offline. Blob contents arrive when a command needs them, such as the checkout of your starting branch. Later commands that touch old versions, such as git blame on a long-lived file, trigger extra fetches. Git batches these requests, but you will notice pauses on a slow link.
If the repository is a monorepo and you work in a few directories, add sparse checkout:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api docs
git sparse-checkout listOnly blobs for services/api, docs, and the top-level files are downloaded. This pair is the best general-purpose setup for large repositories and is the default recommendation for people who work in the repo every day.
Profile 2: code review
A reviewer needs to read a change, not to build on it. The needed objects are the tip of the branch under review and enough history to compute the diff against its base.
For a review of a pull request, a blobless clone works well. The diff between two commits requires only the blobs for the files that changed, and Git fetches exactly those. A full clone would download every old version of every file for a task that touches a handful.
If the review needs only the branch tip and its parent, a shallow clone can do it:
git clone --depth 1 --single-branch --no-tags --branch main https://example.com/org/big-repo.gitThe trouble is the merge base. A review diff is usually computed from the common ancestor of the feature branch and its target. In a depth-1 clone, Git does not have that ancestor, so a three-dot diff (main...feature) can fail or show the whole tree as changed. You have two options. Fetch the feature branch with enough depth to reach the ancestor, using git fetch --deepen 50, or use a blobless clone that already has the full graph. For a reviewer who opens many different branches, the blobless clone is less work.
Profile 3: one-off inspection
Sometimes you only want to look: read a file, check how a build is configured, grep for a symbol, or confirm what a release contains. The clone will be deleted when you finish.
Here a shallow, single-branch clone is the smallest starting point:
git clone --depth 1 --single-branch --no-tags --branch main https://example.com/org/big-repo.gitYou get one commit's worth of files. Searching with git grep or ordinary tools works fully, because the working tree and the blobs for that commit are present.
If you also want to look at a specific historical question, such as when a file was introduced, move to a blobless clone instead of deepening a shallow one. A blobless clone answers git log -- path correctly in one step. A shallow clone would need git fetch --unshallow, which downloads all the blobs too.
For a very large repository where you want only one directory, combine the options:
git clone --filter=tree:0 --depth 1 https://example.com/org/big-repo.gitA treeless, depth-1 clone suits a script that needs the current snapshot and nothing else. Do not use tree:0 for interactive work that browses history. Every git log -- path or git blame then needs trees for many commits, and Git fetches them in many small requests that can be slower than a full clone.
Reading the trade-offs side by side
The charts below compare a typical full clone with the strategies for a large repository. They show the order of magnitude and not measurements, so check your own repository with git count-objects -vH after cloning.
Edge cases that change the answer
Some situations override the simple flow.
- Tools that read history. Build systems that embed a version from
git describeneed tags and enough commits to reach one. A--no-tags --depth 1clone breaks them. Fetch tags and deepen, or pass the version in another way. - Offline work. Partial clones depend on the remote for missing objects. If the remote is unreachable, commands that need those objects fail with an error about a missing object or a failed fetch. A full clone is the only strategy with no network dependency after the clone.
- Long-lived clones. A shallow clone drifts. Each
git fetchadds new commits on top of the boundary, and the repository is never shallower than it was. A blobless clone stays consistent because it keeps the whole graph, and its missing blobs are fetched only on demand. - Server support. Filters need a server that allows them. If a host rejects
--filter, Git prints a warning that the filter is not supported and may fall back to a full clone, so check the size of what you got. - Pushing. Pushing from a blobless clone works normally. Pushing from a shallow clone can fail on servers that need the full parent chain, so test it before relying on it.
You can confirm which strategy a clone is using with these commands:
git rev-parse --is-shallow-repository
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilter
git sparse-checkout listThe first prints true for a shallow repository. The next two show a partial clone and its filter. The last lists the sparse paths. Run them whenever behavior surprises you.
A worked choice
Suppose you join a team with a 20 GB monorepo. You will fix bugs in services/api over the next year and read history often. A full clone costs a long download and a lot of disk. A depth-1 clone would break your blame and rebase work within a week. The sound choice is --filter=blob:none --sparse followed by sparse-checkout set services/api. You keep the whole commit graph, you download only the file contents you use, and you can widen the sparse set at any time with another sparse-checkout set or add.
Now suppose a colleague asks you to check whether a config file in the same repository sets a flag. A one-off --depth 1 --single-branch --no-tags clone answers it in seconds, and you delete it afterward.
Exercise
You review pull requests across many feature branches of a large repository, and each review needs a diff against the merge base. Why is a depth-1 clone a poor choice, and what would you use instead?
Show answer
A depth-1 clone has no history beyond the tip, so Git cannot find the merge base and the three-dot diff fails or shows the whole tree as changed. Use a blobless clone (--filter=blob:none). It keeps all commits and trees, so merge-base computation works for any branch, and it downloads only the blobs for files that changed in each reviewed diff.
Exercise
A teammate clones with --filter=tree:0 for daily work and complains that git log -- path and git blame are slow. What is happening, and what should they change?
Show answer
With tree:0, Git has no trees locally. Path-limited history and blame need the trees of many commits, so Git fetches them on demand in many small requests. The teammate should re-clone with --filter=blob:none, which keeps all trees locally, and add sparse checkout if they work in only part of the repository.