Sub topic 6
Deepening history and converting to a full clone
A shallow clone is a starting point, not a permanent state. This chapter covers how to add history to a shallow clone, how to convert it to a complete repository, and what to check when the result is not what you expected. It assumes you know git clone, git fetch, and branches. It also assumes you have read the earlier chapters on --depth and on what changes in a shallow repository.
How Git remembers the shallow boundary
A shallow repository stores a file named .git/shallow. It lists the commits whose parents were deliberately not downloaded. Git treats these boundary commits as if they had no parents. Every command that walks history stops there.
Deepening a clone means fetching the parents of the boundary commits and moving the boundary further back. Unshallowing means fetching all missing ancestors and deleting the boundary entries. Both are ordinary fetches that negotiate with the server about which commits you already have.
You can check the state at any time:
git rev-parse --is-shallow-repository
cat .git/shallowThe first command prints true or false. The second lists the boundary commit hashes, and the file does not exist in a complete repository.
Deepening by a fixed number of commits
git fetch --deepen=<n> adds n commits beyond the current boundary. It is relative to where your history stops now, not to the branch tip.
git fetch --depth=<n> is different. It is measured from the tip of each remote branch. If new commits have arrived upstream, the depth is counted from the new tip. A clone at depth 1 that runs git fetch --depth=50 ends up with the latest 50 commits.
The following script builds a local repository with ten commits and walks through each step. It clones with a file:// URL because Git ignores --depth when you clone from a plain local path.
set -e
rm -rf /tmp/demo && mkdir /tmp/demo && cd /tmp/demo
git init -q -b main origin
cd origin
for i in $(seq 1 10); do
echo "line $i" >> notes.txt
git add notes.txt
git -c user.name=Demo -c [email protected] commit -q -m "commit $i"
done
cd ..
git clone -q --depth 3 file:///tmp/demo/origin shallow
cd shallow
git rev-parse --is-shallow-repository
git rev-list --count HEAD
git fetch -q --deepen 4
git rev-list --count HEAD
git fetch -q --unshallow
git rev-list --count HEAD
git rev-parse --is-shallow-repositoryThe script prints true, 3, 7, 10, and false. The clone starts with 3 commits. Deepening by 4 gives 7. Unshallowing fetches the remaining 3 and the repository is no longer shallow.
Deepening by date or by ref
A commit count is often the wrong unit. You usually want "everything since the last release" or "everything from the last quarter". Two options cover that:
git fetch --shallow-since=2026-01-01
git fetch --shallow-exclude=v2.0.0--shallow-since fetches history newer than the given date. --shallow-exclude fetches history reachable from your branches but not from the named ref, such as a tag. Both work on clone and on fetch. They are useful for changelog tools, which need history back to a specific release and nothing earlier.
Depth counts graph distance, not commits
--depth 10 does not promise ten commits. Git counts steps through the commit graph, and a merge commit has more than one parent. A history with many merges can therefore contain more than ten commits at depth 10. If a script compares git rev-list --count HEAD to the depth you requested, expect mismatches on branchy history.
Converting to a full clone
git fetch --unshallow downloads every missing ancestor of the refs you track and removes the shallow marker. On a large repository this is the same transfer you avoided by cloning shallow. It has no shortcut, so decide whether you need the full history before you start.
Running --unshallow on a complete repository fails:
fatal: --unshallow on a complete repository does not make senseThis error is harmless. Scripts that must work either way should test first:
if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
git fetch --unshallow
fiYou may see advice to use git fetch --depth=2147483647 as an older equivalent of --unshallow. It works, but --unshallow states the intent.
Unshallowing does not restore other branches
A clone made with --depth implies --single-branch. Its fetch refspec covers only one branch. Unshallowing downloads the full history of that branch and nothing else. To get all branches, widen the refspec first:
git remote set-branches origin '*'
git fetch --unshallowTo add just one branch, use --add:
git remote set-branches --add origin release/2.0
git fetch --depth=1 origin release/2.0The second command keeps the new branch shallow. Without it, Git fetches the whole history of that branch.
Tags work in a similar way. A clone made with --no-tags stores no tags, and a later unshallow does not add them. Run git fetch --tags when you need them.
Combining with partial clones
If the repository is also a partial clone, for example one created with --filter=blob:none, --unshallow still obeys the filter. You get the missing commits and their trees, but file contents stay on the server until a command needs them. The two features are independent. Shallow limits how many commits you have. The filter limits which object types you have. Removing one does not remove the other.
Commands that tell you to deepen
Many problems in shallow clones come from history that stops too early. These are the usual signs:
| Symptom | Cause | Fix |
|---|---|---|
| `git describe` reports `fatal: No tags can describe` or an odd distance | The nearest tag is beyond the boundary, or tags were never fetched | `git fetch --tags --deepen=N`, or `--unshallow` |
| `git log` stops after N commits | The boundary is acting as the root commit | Deepen |
| `git blame` blames the boundary commit for most lines | The real author's commit was not downloaded | Deepen, or unshallow before blaming |
| `git merge-base` prints nothing, or a merge says `refusing to merge unrelated histories` | The common ancestor is past the boundary | Deepen until the branches meet |
| `git diff main...feature` shows too much or errors | The merge base is missing | Deepen both refs |
For a pull request check, the merge base is typically a few dozen commits back. Deepen in steps until git merge-base returns a commit, rather than unshallowing a huge repository at once:
git fetch --deepen=50 origin main feature
git merge-base origin/main origin/featureIf the merge base prints a commit hash, stop. If it prints nothing, repeat with a larger number.
Pushing from a shallow clone
Modern Git can push from a shallow clone. Older Git versions refused with errors about missing objects. A push can still fail if the server needs history that you do not have, for example when you push a new branch based on a commit that the server lacks. In that case, deepen so that your branch connects to history the server knows.
Checking the result
After any deepening step, confirm what you have:
git rev-parse --is-shallow-repository
git rev-list --count HEAD
git log --oneline | tail -n 1The last line shows the oldest commit you can see. In a shallow repository it is the boundary commit, which looks like a root commit even though it has real parents on the server.
Summary of the options
| Goal | Command |
|---|---|
| Add N commits beyond the current boundary | `git fetch --deepen=N` |
| Set depth N from the current remote tip | `git fetch --depth=N` |
| Add history since a date | `git fetch --shallow-since=DATE` |
| Add history back to a tag or ref, excluding it | `git fetch --shallow-exclude=REF` |
| Get complete history of tracked branches | `git fetch --unshallow` |
| Track all branches before unshallowing | `git remote set-branches origin '*'` |
Exercise
A CI job clones with git clone --depth 1. A later step runs git merge-base origin/main HEAD on a feature branch and prints nothing. Which command is the least costly fix, and why would git fetch --unshallow be a poor first choice?
Show answer
Run git fetch --deepen=50 origin main HEAD (or fetch the feature branch by name) and repeat with a larger number until git merge-base prints a commit. At depth 1 the two branch tips have no shared commit in the downloaded history, so the merge base is missing. Deepening adds only the commits needed to connect them. --unshallow downloads the full history of the tracked branches, which is the cost the shallow clone was meant to avoid.
Exercise
You cloned with git clone --depth 5 --branch main URL, then ran git fetch --unshallow. You now want the branch release/2.0 with full history too. What do you run, and why does git fetch --unshallow alone not give you the branch?
Show answer
Run git remote set-branches --add origin release/2.0 followed by git fetch origin release/2.0. A clone with --depth implies --single-branch, so the fetch refspec names only main. Unshallowing completes the history of the branches the refspec names, and release/2.0 is not one of them until you add it.