Sub topic 4
Shallow clones with --depth
A shallow clone downloads only the most recent part of a repository's history. This chapter covers how --depth works, what it changes inside the repository, which commands behave differently afterward, and how to deepen a clone later. It assumes you can clone, fetch, commit, and branch. It also assumes you know that Git stores history as commits, trees, and blobs. The previous chapters explained why a full clone of a large repository is costly. Here we use the first tool for cutting that cost.
What --depth does
A normal clone fetches every commit reachable from every branch. A shallow clone sets a cutoff. With --depth N, Git asks the server for the tip commit of the branch and its ancestors, going back N commits along the history. Older commits are not sent. The trees and blobs that only those older commits referenced are not sent either.
git clone --depth 1 https://example.com/org/big-repo.git
cd big-repo
git rev-parse --is-shallow-repository
git log --onelinegit rev-parse --is-shallow-repository prints true for a shallow repository. It needs Git 2.15 or later. With depth 1, git log --oneline shows a single commit.
The commit at the cutoff is called the boundary commit. It exists in full, with its tree and files, but Git does not have its parents. Git records the boundary commits in a plain text file, .git/shallow. Every command that walks history reads that file and treats those commits as if they had no parents.
Limiting refs at the same time
--depth already implies --single-branch. Git fetches only the history of the branch HEAD points to on the remote, and later fetches update only that branch. You can be explicit and also skip tags:
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. --branch accepts a branch name or a tag name. It does not accept an arbitrary commit hash.
Other ways to set the cutoff
Counting commits is not the only option. Two other options cut by history shape:
--shallow-since=<date>keeps commits newer than the date, for example--shallow-since=2025-01-01.--shallow-exclude=<ref>keeps everything that is not reachable from the given branch or tag. For example,--shallow-exclude=v1.0fetches history after that tag.
The count is measured from the tip you fetch, along every parent. With merge commits, depth 3 can include more than three commits because Git follows both parents of a merge to the same depth.
A runnable demonstration
You can reproduce the behavior without a remote server. One detail matters: for a plain local path, Git ignores --depth and warns you. Use a file:// URL so the clone goes through the normal transfer protocol.
git init demo-origin
cd demo-origin
git config user.name "Demo"
git config user.email "[email protected]"
for i in 1 2 3 4 5; do
echo "line $i" >> notes.txt
git add notes.txt
git commit -m "commit $i"
done
cd ..
git clone --depth 2 "file://$PWD/demo-origin" demo-shallow
cd demo-shallow
git rev-parse --is-shallow-repository
git rev-list --count HEAD
cat .git/shallow
git blame notes.txtYou should see true, a count of 2, and one commit hash in .git/shallow. git blame credits lines 1 to 4 to the older of the two commits, because that is the earliest commit Git has. Only line 5 is credited to the newest commit. The earlier authors are not lost. They are simply not in this clone.
What breaks or changes
A shallow repository is a valid repository. You can commit, branch, and merge in it. The differences appear when a command needs history that is not there.
| Command or task | Behavior in a shallow clone |
|---|---|
| `git log` | Stops at the boundary commit. |
| `git blame` | Credits every line older than the boundary to the boundary commit. |
| `git describe` | Fails or reports a distance that is wrong, if no tag is reachable in the fetched history. |
| `git merge-base`, `git diff A...B` | Fail or give wrong answers if the common ancestor is below the boundary. |
| `git bisect` | Cannot search below the boundary. |
| `git log -S`, `git log --follow` | Search only the fetched commits. |
| `git push` | Works in modern Git, but the server must accept the commits. History-dependent checks can fail. |
Many failures share a cause. A command needs a commit that Git does not have, and Git cannot ask for it automatically the way it can in a partial clone. A shallow clone has no mechanism to fetch missing commits on demand. You have to deepen it yourself.
Deepening and unshallowing
You can move the cutoff after cloning. Three commands cover most cases:
git fetch --deepen 50
git fetch --unshallow
git fetch --depth 100 origin main--deepen 50adds 50 more commits below the current boundary. It is relative to where the clone stands now.--unshallowfetches all remaining history, and the repository is no longer shallow. It fails on a repository that is already complete.--depth 100is absolute, measured from the tip of the fetched branch. It can also make a clone shallower. Fetching with a smaller depth moves the boundary toward the tip.
To check progress after deepening, run git rev-list --count HEAD and compare the count before and after.
Getting other branches
Because the clone is single-branch, git branch -r shows only one remote branch. To work with another branch, widen the refspec and fetch it:
git remote set-branches origin '*'
git fetch --depth 1 origin feature/login
git switch feature/loginWithout the --depth option on that fetch, Git would download the full history of the new branch. Passing it keeps the new branch shallow too.
Edge cases to know
- Local clones ignore depth.
git clone --depth 1 ./repoprints a warning and copies everything. Usefile://. - Submodules are not shallow by default. Add
--shallow-submodulesto apply the depth to them. - Tags may be missing. A tag is fetched only if it points at a commit in your history.
--no-tagsremoves even those. - Shallow does not shrink the working tree. You still check out every file at the tip. If the tree itself is large, depth alone will not help. Sparse checkout and filters address that.
- Fetching into a shallow clone can get larger over time. Each
git pulladds the commits since your last fetch, and your boundary stays where it was. A long-lived shallow clone slowly grows back toward full history. Re-clone, or re-rungit fetch --depth 1, to reset it.
When depth 1 is the right choice
Use a shallow clone when you need current files and nothing else. Typical cases are a build, a test run, a deployment, or a quick look at the code. Avoid it for development that involves blame, bisect, release tagging, or merge-base calculations. A later chapter returns to this decision with all the strategies side by side.
Exercise
You cloned with git clone --depth 1 --single-branch --branch main <url>. A script then runs git describe --tags and prints only a commit hash. Why, and what is the smallest change that lets it print a tag-based name?
Show answer
With depth 1 only the tip commit is present, and tags are fetched only if they point at commits you have. The latest tag sits on an older commit that was not downloaded, so git describe has nothing to count from. Deepen the clone until the tagged commit is included, for example with git fetch --deepen 50 repeated as needed, or git fetch --shallow-exclude=<tag> style limits, or git fetch --unshallow if the tag is far back. If the tag is on a commit already fetched but missing locally as a ref, git fetch --tags also helps.
Exercise
You run git clone --depth 1 ./my-local-repo copy and git rev-parse --is-shallow-repository prints false. What happened, and how do you get a shallow clone?
Show answer
For a plain local path, Git ignores --depth and copies the whole repository, with a warning. Use a file:// URL instead, for example git clone --depth 1 "file://$PWD/my-local-repo" copy.