Sub topic 15
Inspecting and Troubleshooting Shallow and Partial Repositories
This chapter assumes you can clone, fetch, and branch, and that you know Git stores commits, trees, and blobs. It also assumes you have met --depth, --filter, and sparse checkout in earlier chapters. Here you learn to tell what kind of repository you have, read the signs of trouble, and fix the usual problems.
Identify what kind of repository you have
Before you fix anything, find out whether the repository is shallow, partial, sparse, or a mix. Each state leaves a different trace in the repository's configuration or metadata.
| Question | Command | What it tells you |
|---|---|---|
| Is it shallow? | `git rev-parse --is-shallow-repository` | Prints `true` or `false` |
| Where are the history cut points? | `cat .git/shallow` | Lists the commits whose parents were not downloaded |
| Is it a partial clone? | `git config --get remote.origin.promisor` | Prints `true` if the remote is a promisor remote |
| Which filter was used? | `git config --get remote.origin.partialclonefilter` | Prints the filter, such as `blob:none` |
| Which paths are checked out? | `git sparse-checkout list` | Lists the directories in the sparse set |
A promisor remote is a remote that promised to supply any object the local repository lacks. Git records this in the configuration when you clone with --filter.
git rev-parse --is-shallow-repository
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilter
git sparse-checkout listA repository can be several of these at once. A CI checkout might be shallow, filtered, and sparse. Run all four commands before you decide what is wrong.
Problems caused by shallow history
A shallow repository has commits whose parents are missing. Any command that walks past the cut point sees a truncated history.
Missing history
git log stops at the boundary. git log --oneline on a depth-1 clone shows one commit, and that is correct behavior. The same applies to git blame, which attributes every line that has no earlier visible commit to the boundary commit. If a blame result looks wrong, check git rev-parse --is-shallow-repository first.
Merge base failures
Commands that compare branches need a common ancestor. If the two branches were fetched at a shallow depth, the ancestor may not exist locally. Typical symptoms are:
git merge-base origin/main HEADprints nothing and exits with status 1.git diff origin/main...HEADfails withno merge base.- A tool that lists changed files for a pull request reports every file as changed.
The fix is to fetch more history until the two lines meet:
git fetch --deepen 50
git merge-base origin/main HEADIf you do not know how far back the common ancestor is, repeat the deepen step, or convert the repository to a full one:
git fetch --unshallow--unshallow fails with an error on a repository that is not shallow. In scripts, guard it:
if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
git fetch --unshallow
fiTags and version strings
git describe walks back through history until it finds a tag. In a clone made with --no-tags or too little depth, it fails with No names found or reports a commit hash alone. Build scripts that embed a version from git describe then produce wrong or empty version strings. Fetch the tags and enough depth:
git fetch --deepen 200 --tags
git describe --tagsIf the tag is still not found, the tagged commit is deeper than 200 commits. Deepen further or unshallow.
Fetching a single branch
A clone made with --single-branch records only one branch in its fetch rules. Asking for another branch then fails with couldn't find remote ref. Add the branch explicitly:
git remote set-branches --add origin release-2
git fetch --depth 1 origin release-2Problems caused by partial clones
A partial clone is not missing history. It is missing objects, and Git fetches them when a command needs them. Most partial-clone problems are really surprises about when and how those fetches happen.
Unexpected on-demand fetches
With --filter=blob:none, git log and git log --stat behave differently:
git log --oneline -- README.mdreads commits and trees only. It runs offline.git log -pneeds file contents for every commit, so Git fetches many blobs, often in batches.git blameon a large file can trigger many fetches because it needs old versions of the file.git checkoutof an old commit fetches the blobs of that commit.
A command that seems to hang is often downloading missing objects. To see this, turn on tracing:
GIT_TRACE=1 git log -p -5 -- README.md 2>&1 | grep -i fetchLines that mention fetch or promisor show the lazy fetches as they happen. Check which paths are checked out with git sparse-checkout list too. If the sparse set is larger than you meant, more blobs download at checkout time.
Working offline
Lazy fetching needs the remote. Offline, a command that needs a missing object fails with an error such as fatal: could not fetch <hash> from promisor remote. You can still run commands that use only local objects. To prepare for offline work, fetch what you need first. This command downloads all blobs for the current commit, which a checkout already did, plus the blobs of recent history:
git fetch --refetch--refetch re-downloads everything and applies the current filter again. Use it only when you want to change the filter or repair damage, because it transfers a lot of data. To remove the filter completely and make the repository a normal full clone, use the same command with an empty filter:
git config --unset remote.origin.partialclonefilter
git config --unset remote.origin.promisor
git fetch --refetchCheck the exact steps for your Git version, since --refetch needs Git 2.36 or later.
Treeless clones and history commands
A --filter=tree:0 clone has commits but no trees. Almost every command that looks at file names must fetch trees on demand, and for history commands this can mean one fetch per commit. For this reason a treeless clone suits a build that needs only the current snapshot, and it is a poor choice for a developer who runs git log on paths. If a treeless clone feels slow in daily use, re-clone with --filter=blob:none.
The server rejects the filter
If the server does not support filtering, the clone prints a warning such as warning: filtering not recognized by server, ignoring and downloads everything. The result is a normal full clone, with no error. The server must enable uploadpack.allowFilter, and many hosting services do this already. Confirm after cloning:
git config --get remote.origin.promisorIf this prints nothing, the filter was ignored. Ask the server administrator to enable filters, or fall back to --depth and sparse checkout.
Problems caused by sparse checkout
Files outside the sparse set do not exist in the working tree. This is intended, but it produces confusing errors.
- A build fails with
No such file or directoryfor a path in a sibling directory. Add the directory:git sparse-checkout add shared/libs. git statusdoes not report files outside the set, even if they changed upstream.git sparse-checkout listshows nothing in a repository where you expected a set. Cone mode may be off, or sparse checkout may never have been initialized.
In a partial clone, adding a directory to the set triggers a download of its blobs. That is the correct behavior, but a very large directory can take a while.
A short troubleshooting routine
- Run the four inspection commands from the table above.
- Match the symptom. Truncated history or a missing merge base points to shallow. Slow commands or promisor errors point to partial. Missing files point to sparse.
- Apply the smallest fix:
--deepen, then--unshallow, then a sparse set change, then--refetch. - Run the original command again and confirm the symptom is gone.
Exercise
A CI job clones with git clone --depth 1, then runs git diff origin/main...HEAD --name-only and fails with no merge base. Name the cause and give two commands that fix it.
Show answer
The repository is shallow, so the common ancestor of the two branches was never downloaded. Run git fetch --deepen 50 and repeat it until git merge-base origin/main HEAD prints a commit, or run git fetch --unshallow to get the full history.
Exercise
You cloned with --filter=blob:none, and git config --get remote.origin.promisor prints nothing. What most likely happened, and what should you check next?
Show answer
The server probably does not support filtering, so Git ignored the filter and made a full clone. Check the clone output for the warning filtering not recognized by server, and ask the server administrator to enable uploadpack.allowFilter, or use --depth with sparse checkout instead.
Exercise
In a blobless clone, git log --oneline -- README.md is instant, but git log -p -- README.md is slow. Why?
Show answer
The first command needs only commits and trees, which are local. The second needs the file contents of every commit that touched the file, so Git fetches those missing blobs from the promisor remote.