Sub topic 5
What breaks or changes in a shallow repository
This chapter assumes you can clone, fetch, branch, and merge with Git, and that you know a commit points to its parent commits. A shallow clone, made with git clone --depth N, keeps only the newest N commits on each fetched branch. Everything older is absent. That gap changes the answers of several commands, and a few commands stop working. This chapter lists which ones, explains why, and shows how to notice the problem early.
How Git represents the cut
A commit object always stores the hashes of its parents. A shallow clone does not rewrite those commits. Instead, Git records the oldest commits it has in a file named .git/shallow, one hash per line. When Git walks history and reaches a commit listed there, it behaves as if that commit had no parents.
Two consequences follow:
- Commit hashes are identical to those in the full repository. Signatures, tags, and references to a commit stay valid.
- Every command that walks history stops at the boundary without any error. The truncated result looks like a normal result, so the failure is easy to miss.
See it in a runnable example
The script below builds a five-commit repository with a tag on the first commit. It then makes a depth-2 clone and runs the commands that behave differently.
set -e
rm -rf /tmp/demo-origin /tmp/demo-shallow
git init -q -b main /tmp/demo-origin
cd /tmp/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 -q -m "commit $i"
if [ "$i" = 1 ]; then git tag v1.0; fi
done
git clone -q --depth 2 file:///tmp/demo-origin /tmp/demo-shallow
cd /tmp/demo-shallow
git rev-parse --is-shallow-repository
git rev-list --count HEAD
git log --oneline
cat .git/shallow
git describe --tags || echo "describe failed"
git describe --tags --always
git blame notes.txtExpected results:
--is-shallow-repositoryprintstrue.rev-list --count HEADprints2, not5.git loglists only "commit 5" and "commit 4"..git/shallowcontains the hash of commit 4, the boundary.git describe --tagsfails with "No names found", because tagv1.0points at commit 1, which was never downloaded. The tag was not fetched either.git describe --tags --alwaysprints an abbreviated hash instead.git blamecredits lines 1 to 4 to commit 4, usually marked with a leading^. Those lines were really written by commits 1 to 4.
Commands that change behavior
| Command or task | What happens in a shallow clone | Why |
|---|---|---|
| `git log`, `git shortlog` | Output stops at the boundary commit. | Parents past the boundary are hidden. |
| `git rev-list --count HEAD` | Returns at most the depth, so it is not the project's commit count. | Only the downloaded commits are counted. |
| `git blame` | Lines older than the boundary are credited to the boundary commit. | The boundary commit looks like the first commit. |
| `git describe` | Fails, or reports the wrong distance, when the nearest tag is outside the downloaded history. | Tags beyond the boundary are unreachable. |
| `git log v1.0..HEAD` | Fails with "unknown revision" if `v1.0` was not fetched. | The tag or its commit is missing. |
| `git merge-base A B` | Prints nothing and exits with status 1 if the common ancestor is past the boundary. | The shared commit is not present. |
| `git diff A...B` | Fails with "no merge base". | The three-dot form needs a merge base. |
| `git merge`, `git rebase` | Can refuse with "refusing to merge unrelated histories" or replay too many commits. | Without a common ancestor, the branches look unrelated. |
| `git bisect` | Can search only inside the downloaded range. | A culprit older than the boundary cannot be found. |
| `git log -S` and `git log -G` | Find changes only inside the downloaded range. | Older commits are absent. |
The merge-base problem in practice
Many review and release tasks need the point where a branch left the main line. A depth-1 clone almost never contains that point. This is the most common surprise in pipelines.
git fetch --depth 1 origin main feature
git diff origin/main...origin/featureIn this example, each branch is truncated to its tip commit. The two tips share no downloaded ancestor, so the second command fails with "no merge base". Plain two-dot diffs such as git diff origin/main origin/feature still work, because they compare two snapshots and need no history. The result is different, though. A two-dot diff includes changes made on main since the branch point, which a three-dot diff would leave out.
To use three-dot syntax, deepen the history until the branches meet, or fetch with enough depth in the first place. Later chapters cover deepening and choosing a depth.
Tags and version numbers
Build scripts often derive a version string from git describe or from a commit count. Both are fragile on shallow clones:
- With
--depth, Git fetches only the tags that point into the downloaded commits. With--no-tags, it fetches none. - A version that includes the distance from the last tag, such as
v1.4.2-37-gabc1234, fails or reports a distance that is too short when the tag is outside the boundary. - A build number taken from
git rev-list --count HEADchanges as the clone depth changes. Two builds of the same commit can get different numbers.
Fetch the tag you need explicitly, or generate version numbers from something that does not depend on local history, such as a CI run number or a file that stores the version.
git fetch --depth 1 origin tag v1.0
git describe --tagsThe git fetch --depth 1 origin tag v1.0 command downloads the tagged commit only. After it, git describe --tags can name the tag if the tagged commit is reachable from your current commit. If the tag is not an ancestor of HEAD in the downloaded history, describe still cannot use it.
What still works
Shallow repositories are not read-only. Since Git 1.9, you can clone from a shallow repository and push from one. Normal daily work also behaves well:
git commit,git switch,git status, andgit diffbetween commits you have are unaffected.git fetchworks. Without--depth, it keeps the existing boundary and adds new upstream commits on top of it. The clone's depth therefore grows as the remote branch moves.git fetch --depth Nresets the boundary to N commits below each fetched tip. This also moves the boundary forward, which makes the repository smaller again.git pushworks in most cases, but a server can reject a push whose new commits rest on history it lacks. Rejections are rare when you push to the same remote you cloned from.
Visualizing the boundary
Detecting the problem early
Do not wait for a confusing failure. A script that needs history can check for a shallow clone first and decide what to do.
if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
echo "Shallow clone: history is truncated."
git rev-list --count HEAD
fiUse the printed count to compare the available depth with what your task needs. If it is not enough, deepen the clone before running describe, merge-base, or blame. The next chapter shows git fetch --deepen and git fetch --unshallow for that.
Common mistakes
- Trusting an empty result.
git merge-baseprinting nothing means "no common commit found here," which can be a shallow artifact and not a true statement about the branches. - Reading
git blameon a shallow clone as authorship. Treat boundary attributions as "this line is at least this old." - Using a commit count as a version number.
- Testing with a local path, seeing a full clone, and being surprised when CI behaves differently.
Exercise
A CI job clones with git clone --depth 1, then runs git diff origin/main...HEAD to find the files changed on a pull request branch. The command fails with "no merge base". Explain why it fails, and name one command form that still works but gives a different answer.
Show answer
At depth 1 the job has only the tip commit of one branch, and the main branch tip is a separate truncated commit. Git finds no downloaded commit that both share, so the three-dot form, which diffs from the merge base, has nothing to start from. The two-dot form git diff origin/main HEAD still works because it compares two snapshots without needing history, but it also includes changes made on main since the branch point. A fix that keeps the three-dot form is to deepen the clone until the branch point is downloaded.