Why the answer is wrong#
A shallow clone stops at a boundary. CI systems create them by default: actions/checkout fetches "only a single commit" unless you set fetch-depth. Git records the boundary commits in .git/shallow and treats them as if they had no parents.
git merge-base --is-ancestor A B walks back from B looking for A. Its documentation defines the result by exit status: 0 if A is an ancestor of B, 1 if not, and any other non-zero status for an error. In a shallow clone the walk stops at the boundary. Two things can then happen, and neither is the true answer.
- If A is not in the clone at all, Git cannot resolve the name and exits 128.
- If A is in the clone, because a tag, a branch or a separate fetch brought it in, but the history between A and B is missing, the walk never reaches A and Git exits 1: "not an ancestor".
The second case is the dangerous one. Exit 1 is the documented answer for "no", so a script that checks if git merge-base --is-ancestor ... has no way to tell a real "no" from a missing history.
The lab#
The lab needs Git only and runs in a second. It builds a 50-commit history, clones it at depth 1, and asks whether commit 10 is an ancestor of the tip.
git init -q -b main origin-repo && cd origin-repo
for i in $(seq 1 50); do echo $i > n.txt; git add n.txt; git commit -qm "commit $i"; done
OLD=$(git rev-parse HEAD~40)
cd ..
git clone -q --depth 1 file://$PWD/origin-repo ci-clone && cd ci-clone
git rev-parse --is-shallow-repository
git merge-base --is-ancestor $OLD HEAD; echo "exit=$?"
git fetch -q --depth 1 origin $OLD
git merge-base --is-ancestor $OLD HEAD; echo "exit=$?"
git fetch -q --unshallow origin
git merge-base --is-ancestor $OLD HEAD; echo "exit=$?"
With Git 2.43:
true
fatal: Not a valid commit name 680582276762ea8eb40e89033489d5eaf6197398
exit=128
exit=1
exit=0
The same question gets three answers. Before the old commit is present, Git refuses. After fetching that one commit, Git confidently says no. Only after --unshallow does it say yes, which is the truth.
Start any ancestry check with git rev-parse --is-shallow-repository. If it prints true, either refuse to answer or fetch more history first. Never read exit 1 from a shallow clone as "no".
Option 1: fetch the history#
git fetch --unshallow turns the clone into a complete one. It is the simplest fix and the most expensive: on a large repository it downloads every commit, tree and blob in the history. --deepen=N and --shallow-since=DATE fetch less, and suit a question with a known horizon, such as "was this commit merged in the last week".
A partial clone is often the better trade. git clone --filter=tree:0 fetches every commit but no trees or blobs until they are needed, so ancestry checks run locally and the clone stays small. The server must allow filters; GitHub describes its support for partial clones. In the lab, with uploadpack.allowFilter set on the source repository:
$ git clone -q --filter=tree:0 file://$PWD/origin-repo treeless && cd treeless
$ git rev-parse --is-shallow-repository
false
$ git merge-base --is-ancestor $OLD HEAD; echo "exit=$?"
exit=0
$ git rev-list --count HEAD
50
A later command that needs file contents will fetch them on demand, which is slow in a loop. The partial clone guide covers what such a clone holds and what it must still fetch.
Option 2: ask the hosting service#
If the clone exists only to answer the question, skip the history altogether. GitHub's compare two commits endpoint answers it for any two commits in a repository network. Its status field is one of ahead, behind, identical and diverged, describing the second commit relative to the first. So for compare/A...B:
aheadoridentical: B contains A.behindordiverged: it does not.
The response can be large, because the first page lists up to 300 changed files with their patches. The same reference says the file list "is only shown on the first page of results". Asking for the second page at one commit per page returns the status and comparison fields without the file list:
gh api "repos/example-org/example-service/compare/$OLD...$TIP?per_page=1&page=2" --jq .status
The repository name is invented. Cache answers per pair: several questions often share a commit, and each read counts against the API rate limit. A failed read must stay a failure. If the API cannot answer, exit with an error instead of falling back to the local clone, or you reintroduce the shallow answer you set out to avoid.
Which to choose#
- One ancestry question, or a few, per job: ask the hosting service. It costs one small request each.
- Many questions over recent history: fetch with
--shallow-sinceor--deepento cover the window, then check--is-shallow-repositorybefore trusting a "no" from beyond it. - Questions plus other history work, such as
git logover the range: use a treeless clone. - A full
--unshallow: only when the job needs the whole history anyway.
Whichever you choose, the commit you ask about matters as much as the method. For a pull request, the head or merge commit guide explains which commit CI actually built.
In production#
In the case behind this guide, a release tool answered "which production release first carried this commit" by asking, for each release candidate, whether its recorded commit contained the one in question. It used the local clone, and refused when the clone was shallow, which is how automated sessions check the repository out. Unshallowing that clone took 90.7 seconds and grew .git from 175 MB to 554 MB. The fix asks the compare endpoint instead, reads page 2 at one commit per page, which returned about 14 KB in place of a 300-file listing, and caches each answer per commit pair. On a released commit, a commit on the main branch but not yet released, and a commit not on the main branch, the new tool in a shallow clone and the old tool in a full clone gave the same three answers.
Where this came from#
Proofline found this in its own release tooling and changed that tooling on 18 November 2026; the change went out with that day's release. The 90-second unshallow and the 14 KB compare read above are from that change. Proofline reviews GitHub pull requests; new organizations get one calendar month of hosted code review without a cloud connection. Start a free GitHub review ยท Eligibility and fair-use terms
Questions#
Why does git merge-base say "Not a valid commit name" in CI?
The commit is not in the clone, usually because CI fetched only the tip. Fetch more history, or ask the hosting service whether one commit contains the other.
Is fetch-depth: 0 in actions/checkout the fix?
It works, because it fetches all history for all branches and tags. On a large repository it is also the slowest option; a treeless clone or one API request per question is usually cheaper.
Does a shallow clone ever give a wrong "yes"?
Not for ancestry: if Git finds a path from B back to A in the history it has, that path exists. The wrong answers are false "no" results and errors.