Sub topic 5
What breaks or changes in a shallow repository
This chapter assumes you can clone, fetch, branch, and read git log, and that you know a commit points to its parent commits. A shallow clone cuts that chain of parents at a chosen depth. Commands that walk history then behave differently, and some fail.
The shallow boundary
When you run git clone --depth 1, Git records the cut-off commits in a file named .git/shallow. Each commit listed there is treated as if it had no parents, even though it did in the original repository. Git calls these commits the shallow boundary.
git clone --depth 3 https://example.com/org/big-repo.git
cd big-repo
cat .git/shallow
git log --onelinegit log stops at the boundary. It shows three commits and no error, so a short history can be mistaken for a young project.
Commands that change behavior
The table lists common commands and what happens at the boundary.
| Command | Behavior in a shallow repository |
|---|---|
| `git log`, `git shortlog` | Stops at the boundary. Counts and authors are incomplete. |
| `git blame` | Blames every line from older commits on the boundary commit. |
| `git describe` | Fails or gives a misleading result if no tag is reachable in the fetched history. |
| `git merge-base` | Returns nothing or the wrong commit if the true common ancestor is cut off. |
| `git bisect` | Cannot search past the boundary. |
| `git rev-list --count HEAD` | Returns the shallow count, not the real one. |
The blame result is easy to misread. The boundary commit looks like it wrote every older line, so the output is wrong and gives no warning.
Build tools that depend on history
Many build scripts read history without saying so. Version strings made from git describe, changelog generators, and "what changed since the main branch" checks all need commits that a shallow clone may lack.
A diff against another branch needs the merge base. This often fails in a depth-1 clone:
git clone --depth 1 --branch feature https://example.com/org/big-repo.git
cd big-repo
git merge-base origin/main HEADWith --depth 1 the clone has only one branch tip. If origin/main was not fetched, the command prints nothing and exits with an error. If you fetch main at depth 1, the two tips share no visible ancestor, so the merge base is still missing.
Fetching and pushing
Fetching into a shallow repository works, but new commits are fetched relative to the boundary. Pushing from a shallow repository is allowed on modern Git, though it can be refused by servers that cannot verify the missing ancestry. If a push fails with a message about shallow updates, deepen the clone first.
Fixing missing history
Use --deepen to add history in steps, or --unshallow to fetch everything. The next chapter covers both in detail.
git fetch --deepen 50
git rev-parse --is-shallow-repositoryThe second command prints true while a boundary remains and false once the full history is present.
Exercise
In a depth-1 clone, git blame README.md shows the same commit and author for every line. Why, and how do you get accurate output?
Show answer
The only commit present is the boundary commit, which Git treats as having no parents. Blame cannot look further back, so it assigns every line to that commit. Deepen the history with git fetch --deepen N or git fetch --unshallow, then run blame again.
Exercise
A build script computes a version with git describe --tags and fails in CI after you switched to --depth 1. What is the likely cause?
Show answer
The tag points to a commit outside the fetched history, so git describe cannot reach any tag from HEAD. Fetch more history, or fetch the tags and enough commits to reach one, and then rerun the command.