Sub topic 13
Choosing a strategy for development, review, and inspection
This chapter assumes you know how to clone, fetch, and branch in Git. It also assumes you know that Git stores commits, trees, and blobs, and that you have seen --depth, --filter, and sparse checkout in earlier chapters. Here you pick among them based on what you need to do with the repository.
Start from the work, not the flag
Each option trades something away. Shallow clones drop history. Blobless and treeless clones drop file contents or directory listings until Git needs them. Sparse checkout limits which files appear in your working tree. Ask what the task needs:
- Do I need history (log, blame, merge-base)?
- Do I need to work offline?
- Do I need only part of the tree?
- Will I keep this clone for days, or throw it away after one run?
Local development
For daily work you want full history commands to work and you want to stay productive offline after the first fetch. A blobless clone fits this well. It downloads every commit and tree, so git log, git blame on tree structure, and branch operations work without per-commit downloads. Blobs are fetched on demand when you check out or diff.
git clone --filter=blob:none https://example.com/org/big-repo.git
cd big-repo
git log --oneline -- README.mdIf you work in one area of a monorepo, add sparse checkout:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api docsAvoid a shallow clone for development. Commands such as git log, git blame, and git merge-base stop at the shallow boundary, and merging branches can fail or give misleading results.
Code review
Reviewing a branch means comparing it with its base, so you need the history between them. A shallow clone with a small depth often misses the merge base. Two choices work:
- Use a blobless clone, then fetch the branch under review. Diffs download only the blobs they touch.
- Use a shallow clone and run
git fetch --deepen 50untilgit merge-basefinds the common ancestor.
The blobless route is more predictable. The shallow route is smaller if the branch is short-lived and close to the base.
One-off inspection
If you only want to read the current files, run a build once, or grep the code, you do not need history. Use the smallest clone:
git clone --depth 1 --single-branch --no-tags --branch main https://example.com/org/big-repo.gitIf you later discover you need history, run git fetch --unshallow. You do not need to clone again.
Comparison
| Task | Suggested clone | What you give up |
|---|---|---|
| Daily development | `--filter=blob:none`, optionally `--sparse` | Some commands pause to fetch blobs; first offline use may fail on missing blobs |
| Reviewing a branch | `--filter=blob:none` | Same as above |
| Reading or building the current snapshot | `--depth 1 --single-branch --no-tags` | All history |
| History search across all commits | Full clone | Time and disk space |
Changing your mind
Your first choice is not permanent. A shallow clone can be deepened with git fetch --deepen N or converted with git fetch --unshallow. A partial clone keeps fetching what it needs, and you can narrow or widen the checked-out paths with git sparse-checkout set. Starting small and growing is usually cheaper than starting large.
Exercise
You need to review a pull request in a large repository, and git merge-base reports no common ancestor in your --depth 1 clone. What are two ways to fix this, and which is more predictable?
Show answer
Run git fetch --deepen N (repeatedly if needed) until the merge base appears, or re-clone with --filter=blob:none so all commits are present. The blobless clone is more predictable because it has the full commit history, so you do not have to guess a depth.