Sub topic 4
Shallow clones with --depth
A shallow clone downloads only the most recent commits of a branch instead of its whole history. This chapter assumes you know basic Git (clone, fetch, log, branch) and have a rough picture of commits, trees, and blobs. It covers how --depth works, what it saves, and what it takes away.
Why history is the expensive part
A full clone downloads every commit, every tree (directory listing), and every blob (file version) ever recorded. In a large repository, most of that data describes old versions of files that you will never open. A shallow clone cuts the chain of commits at a chosen depth, so Git skips the older commits and the trees and blobs only they reference.
Creating a depth-1 clone
git clone --depth 1 https://example.com/org/big-repo.git
cd big-repo
git rev-parse --is-shallow-repository
git log --oneline--depth 1 fetches only the tip commit of the default branch. The rev-parse command prints true when the repository is shallow. git log shows a single commit, because the history below it was never downloaded.
Git records the cut-off points in a file called .git/shallow. Each line is the ID of a commit whose parents are treated as missing.
flowchart LRLimiting branches and tags too
--depth already implies --single-branch, so it fetches history for one branch only. Adding the following options makes the intent explicit and skips tags as well:
git clone --depth 1 --single-branch --no-tags --branch main https://example.com/org/big-repo.gitThis is the smallest common starting point for a build that needs only the current files.
What changes in a shallow repository
Anything that needs history beyond the boundary gives incomplete answers.
| Task | Behavior in a shallow clone |
|---|---|
| `git log` | Stops at the boundary commit |
| `git blame` | Blames every older line on the boundary commit |
| `git merge-base` | Can fail or return a wrong ancestor if the common ancestor is missing |
| `git describe` | Can fail when no tag is reachable in the downloaded commits |
| Version numbers from commit counts | Wrong, because the count is truncated |
Fetching, committing, and creating branches still work. Pushing from a shallow clone is supported by modern Git, but it can fail on servers that cannot verify the missing history.
Deepening and unshallowing
You can recover history later without cloning again:
git fetch --deepen 50
git fetch --unshallowThe first command adds 50 more commits below the current boundary. The second fetches all remaining history, and the repository is no longer shallow. Use --deepen when you need a bounded amount, for example enough commits to reach a merge base. Use --unshallow when you need everything.
Common errors
- Missing history:
git logorgit blamelooks truncated. Rungit fetch --deepen Nor--unshallow. - Missing branch: a single-branch clone does not fetch other branches. Fetch one explicitly, for example
git fetch --depth 1 origin release. - Version tools fail: tags were skipped by
--no-tags, or the tagged commit is below the boundary.
Exercise
You cloned with git clone --depth 1, and git blame attributes every line of a file to one commit. Why does this happen, and which command fixes it?
Show answer
The older commits that last changed those lines were not downloaded, so Git assigns the lines to the boundary commit. Run git fetch --unshallow to download the full history. If you only need part of it, git fetch --deepen 50 adds 50 commits at a time.