Shallow and Partial Clones
A normal git clone downloads everything: every commit, every folder listing and every version of every file the project ever had. For most projects that is fine. For a repository with years of history or large files, it can mean gigabytes and a long wait. Git offers two ways to download less.
A shallow clone downloads only the most recent commits and leaves out older history. A partial clone downloads the whole history of commits but leaves out file contents (or folder listings) until you actually need them, and then fetches them from the server on demand. Both are used to make cloning faster and smaller, especially on build servers and for very large repositories.
Think of a library. A shallow clone is borrowing only this year's volumes of a journal. A partial clone is taking home the complete catalog of every volume, and asking the library to send a particular volume when you open it.
Syntax
git clone --depth <n> <url>
git clone --shallow-since=<date> <url>
git clone --filter=<filter> <url>| Option | Description |
|---|---|
--depth <n> | It is used to download only the last n commits of each branch that is cloned. |
--shallow-since=<date> | It is used to download only the commits made after a date. |
--single-branch | It is used to download only one branch. --depth turns it on by default. |
git fetch --deepen <n> | It is used to download n more commits of history into a shallow clone. |
git fetch --unshallow | It is used to download the rest of the history, turning a shallow clone into a full one. |
--filter=blob:none | It is used to leave out all file contents until they are needed (a blobless clone). |
--filter=blob:limit=<size> | It is used to leave out only files bigger than a size, such as 1m. |
--filter=tree:0 | It is used to leave out folder listings and file contents until they are needed (a treeless clone). |
The Starting Point
The Fee Tracker team keeps scanned receipts in the repository, and every term adds a larger file. Navika's folder fee-tracker holds the project, and fee-tracker.git stands in for the server, as in "Git Remote". A server must allow filters before partial clones work, so she turns that on (GitHub, GitLab and Bitbucket already do):
mkdir fee-tracker
cd fee-tracker
git init --quiet
mkdir scans
for term in 1 2 3 4 5; do
echo "Term $term fee: \$1${term},000" >> fees.txt
seq 1 $((term * 20000)) > scans/receipts.txt
git add .
git commit --quiet -m "Add the term $term fee and receipt scan"
done
git log --oneline
cd ..
git clone --quiet --bare fee-tracker fee-tracker.git
git -C fee-tracker.git config uploadpack.allowFilter truedf1f5ed (HEAD -> main) Add the term 5 fee and receipt scan
24afe1b Add the term 4 fee and receipt scan
23c295b Add the term 3 fee and receipt scan
a62ac3a Add the term 2 fee and receipt scan
d83476a Add the term 1 fee and receipt scanThe project has five commits, and each one changes both files: a new fee line in fees.txt and a bigger scan in scans/receipts.txt. A full clone of it, for comparison:
git clone --quiet file://$PWD/fee-tracker.git full
git -C full rev-list --all --objects --count25Explanation: git rev-list --all --objects --count counts every object the clone holds: 5 commits, 10 trees (the top folder and scans for each commit) and 10 blobs (five versions of each of the two files). Note the file:// in front of the path. For a plain folder path, Git copies the repository directly and ignores --depth and --filter, so these examples use a file:// URL to make Git behave as it does with a real server.
Shallow Clones
Example 1: Only the Latest Commit
git clone --quiet --depth 1 file://$PWD/fee-tracker.git shallow
cd shallow
git log --oneline
git rev-list --all --objects --count
git rev-parse --is-shallow-repository
cat .git/shallowdf1f5ed (grafted, HEAD -> main, origin/main, origin/HEAD) Add the term 5 fee and receipt scan
5
true
df1f5ed5b0f9e40f116b9ac899e856b3e47f278bExplanation: the clone holds 5 objects instead of 25: one commit, its two trees and the two newest files. The commit is marked grafted: Git knows it has a parent but doesn't have it. git rev-parse --is-shallow-repository confirms it, and the file .git/shallow records the commits where the history is cut. The working files are complete, so you can build, test, edit, commit and push as usual. Only commands that need old history are limited: git log stops at the cut, and git blame blames every line on the grafted commit.
Example 2: Getting More History Later
git fetch --quiet --deepen 2
git log --oneline
git fetch --quiet --unshallow
git log --oneline
git rev-parse --is-shallow-repositorydf1f5ed (HEAD -> main, origin/main, origin/HEAD) Add the term 5 fee and receipt scan
24afe1b Add the term 4 fee and receipt scan
23c295b (grafted) Add the term 3 fee and receipt scan
df1f5ed (HEAD -> main, origin/main, origin/HEAD) Add the term 5 fee and receipt scan
24afe1b Add the term 4 fee and receipt scan
23c295b Add the term 3 fee and receipt scan
a62ac3a Add the term 2 fee and receipt scan
d83476a Add the term 1 fee and receipt scan
falseExplanation: --deepen 2 added two more commits behind the cut, and --unshallow fetched everything else. git rev-parse --is-shallow-repository now prints false: this is a normal clone, and the .git/shallow file is gone. A third way to choose the cut is --shallow-since=2026-01-01 (only commits after a date), and git fetch --depth <n> sets a new depth counted from the tip.
Partial Clones
Example 3: A Blobless Clone
cd ..
git clone --quiet --filter=blob:none file://$PWD/fee-tracker.git partial
cd partial
git log --oneline
git rev-list --all --objects --missing=print | grep -c "^?"
git config --get-regexp "remote.origin.(promisor|partialclonefilter)"df1f5ed (HEAD -> main, origin/main, origin/HEAD) Add the term 5 fee and receipt scan
24afe1b Add the term 4 fee and receipt scan
23c295b Add the term 3 fee and receipt scan
a62ac3a Add the term 2 fee and receipt scan
d83476a Add the term 1 fee and receipt scan
8
remote.origin.promisor true
remote.origin.partialclonefilter blob:noneExplanation: all five commits are there, so git log works fully. But --missing=print shows objects the clone knows about and doesn't have, each marked with ?. There are 8: the four older versions of each of the two files. Git fetched only the file contents needed to check out the newest commit. The two settings mark origin as a promisor remote, a remote that promises to send any missing object when asked.
Example 4: Fetching on Demand
Navika asks for an old version of a file:
git show HEAD~3:fees.txt
git rev-list --all --objects --missing=print | grep -c "^?"Term 1 fee: $11,000
Term 2 fee: $12,000
7Explanation: Git noticed the object was missing, fetched it from origin quietly and printed it. One fewer object is missing now. This is what makes partial clones pleasant: commands work as usual, and only the first time you touch old content does Git need the network. Commands that read many old files, such as git log -p or git blame, will fetch many objects, one batch at a time, so they are slower in a blobless clone.
Other Filters
| Filter | What is left out | Good for |
|---|---|---|
blob:none | All file contents, except for the checked-out commit | Developers: history and blame still work, fetched as needed. The usual choice. |
blob:limit=1m | Only files bigger than 1 MB | Repositories with a few large files. |
tree:0 | Folder listings and file contents | Build servers that check out once and throw the clone away. Slow for history commands. |
Which One to Choose
| Situation | Choose |
|---|---|
| A build server that builds the latest commit and deletes the folder | --depth 1 (or --filter=tree:0) |
A developer on a huge repository who still wants full git log | --filter=blob:none |
| A developer who needs only some folders | --filter=blob:none --sparse, then sparse checkout (see "Git Sparse Checkout") |
| A small or medium project | A normal clone |
Common Mistakes
Using a plain path and wondering why --depth did nothing. Git prints "warning: --depth is ignored in local clones; use file:// instead." Use a file:// URL.
Expecting other branches in a shallow clone. --depth implies --single-branch. Use --no-single-branch, or git remote set-branches origin '*' and fetch.
Using a shallow clone for daily development. Merges and rebases need the merge base, which may be beyond the cut. A blobless clone keeps the whole history and avoids these problems.
Forgetting that a partial clone needs the server. Old content is fetched on demand, so offline work on old history fails.
A server that doesn't allow filters. Git prints "warning: filtering not recognized by server, ignoring" and makes a full clone.
Under the Hood
When Git clones, the client tells the server which commits it wants and the server sends a packfile of everything reachable from them. With --depth, the client also sends a deepen request, and the server sends back the list of commits where it cut the history; the client writes those IDs to .git/shallow and treats them as having no parents. With --filter, the client sends a filter line, and the server leaves matching objects out of the pack. The client records the remote as a promisor (remote.origin.promisor = true), and the received pack is marked with a .promisor file beside it. Later, when any command reads an object that isn't in the repository, Git checks whether a promisor remote might have it and runs a small fetch for just that object (and others needed at the same time, in one batch). This is called a lazy fetch.
Interview Corner
What is the difference between a shallow clone and a partial clone?
A shallow clone (--depth) cuts off old commits: history is incomplete. A partial clone (--filter) keeps every commit but leaves out file contents or folder listings until they are needed, then fetches them from the server on demand.
How do you turn a shallow clone into a full clone?
git fetch --unshallow. To add only some history, use git fetch --deepen <n>.
What does --filter=blob:none do?
It makes a blobless clone: all commits and folder listings are downloaded, but file contents only for the checked-out commit. Older contents are fetched automatically the first time a command needs them.
When would you use --depth 1?
On a build server that needs only the latest code once and then deletes the folder. It is the fastest, smallest clone.
Practice
Challenge: a clone of the last two terms. Make a shallow clone of fee-tracker.git called recent with only the last two commits, and print its log and the contents of .git/shallow.
What does Git print? Before you open the solution, decide how many commits the log shows and which one is marked grafted.
Show solution
cd ..
git clone --quiet --depth 2 file://$PWD/fee-tracker.git recent
git -C recent log --oneline
cat recent/.git/shallowdf1f5ed (HEAD -> main, origin/main, origin/HEAD) Add the term 5 fee and receipt scan
24afe1b (grafted) Add the term 4 fee and receipt scan
24afe1bd1841995fafee5216e1468d380a73302fTwo commits, and the older of them is grafted, because its parent wasn't downloaded. .git/shallow has one line: the ID of that cut-off commit.
Checkpoint. Clone a large public repository three ways (a normal clone, --depth 1 and --filter=blob:none) and compare the time and the size of each .git folder with du -sh .git.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git clone --depth 1 <url> | Clone only the latest commit |
git fetch --deepen <n> | Add n older commits |
git fetch --unshallow | Download the rest of the history |
git clone --filter=blob:none <url> | Clone without old file contents |
git clone --filter=tree:0 <url> | Clone without old folder listings and contents |
git rev-list --all --objects --missing=print | List objects, marking missing ones with ? |
Was this tutorial helpful?