Sub topic 12
Shallow clones with --depth
This chapter assumes you can already run git clone over HTTPS or SSH, and that you know what a commit and a branch are. It builds on the basic clone command and the layout a clone creates.
What a shallow clone is
A normal clone downloads every commit in the repository's history. A shallow clone downloads only the most recent commits, up to a limit you set with --depth. The result is a smaller download and a faster clone.
git clone --depth 1 https://github.com/octocat/Hello-World.gitWith --depth 1, Git fetches only the latest commit on the branch it checks out. You get the full set of files, but no earlier history.
When to use one
Shallow clones suit cases where you need the files and not the past:
- Continuous integration jobs that build or test the latest code.
- Deployment scripts that fetch a release.
- Inspecting a large repository quickly.
They suit everyday development less well, because many history commands need the commits you skipped.
Checking that a clone is shallow
Run these commands inside the clone:
cd Hello-World
git log --oneline
git rev-parse --is-shallow-repositorygit log --oneline shows a single commit. The second command prints true for a shallow repository. Git records the cut-off points in a file named .git/shallow.
Side effects to know about
--depth implies --single-branch. Git fetches only the history of one branch, so git branch -a lists far fewer remote-tracking branches than a full clone would.
Commands that walk history behave differently:
| Command | Effect in a shallow clone |
|---|---|
| `git log` | Stops at the shallow boundary |
| `git blame` | Attributes older lines to the boundary commit |
| `git bisect` | Cannot search past the boundary |
| `git merge-base` | May fail if the common ancestor is missing |
Treat the boundary commit as the oldest commit that exists locally. Older work was not lost. It is still on GitHub.
Getting more history later
You can deepen a shallow clone without starting over. To fetch a set number of additional commits, use --deepen:
git fetch --deepen=10To fetch everything and turn the clone into a full one, use --unshallow:
git fetch --unshallowYou can also limit by date when cloning, which is useful when "the last month" matters more than a commit count:
git clone --shallow-since=2024-01-01 https://github.com/octocat/Hello-World.gitCommon errors
- Missing branches. A branch you expect is absent because of the implied
--single-branch. Add it withgit remote set-branches --add origin BRANCH, then rungit fetch --depth 1 origin BRANCH. - Surprising
git logoutput. Short history is the intended result. Rungit fetch --unshallowif you need more. - Wrong choice for the job. If you need full history but want a smaller download, a partial clone with
--filter=blob:nonekeeps all commits and defers file contents. A shallow clone drops commits instead.
Exercise
You cloned with git clone --depth 1 and now need the full history to run git bisect. Which command do you run, and why does git branch -a show few branches?
Show answer
Run git fetch --unshallow to download the missing history. git branch -a shows few branches because --depth implies --single-branch, so only one branch was fetched.
Exercise
Which command confirms that a repository is shallow?
Show answer
git rev-parse --is-shallow-repository prints true for a shallow repository. A single line from git log --oneline is also a sign.