Skip to content
Latchkey

Git "reference is not a tree" on a shallow clone in CI

A shallow clone only fetches recent history. When a later step asks Git to check out or diff against a commit outside that window, the object is simply absent and Git reports the SHA as "not a tree".

What this error means

A checkout, git merge-base, or git diff against a base SHA or tag fails with fatal: reference is not a tree: <sha>. It works locally with a full clone but breaks in CI where the checkout used fetch-depth: 1.

git
fatal: reference is not a tree: 9fceb02d0ae598e95dc970b74767f19372d61af8

Common causes

Shallow clone missing the target commit

actions/checkout defaults to fetch-depth 1. Any operation needing an older commit (PR base, previous tag) has no object to resolve.

The ref was never fetched

Single-branch or filtered clones omit branches and tags the step later references.

How to fix it

Fetch the history you need

  1. Set fetch-depth: 0 in actions/checkout to get full history, or a depth large enough to include the base.
  2. Alternatively fetch the specific commit or branch on demand.
.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0

Deepen an existing shallow clone

  1. Unshallow in place when full history is needed late in the job.
  2. Or fetch just the missing ref to keep the clone small.
Terminal
git fetch --unshallow
# or fetch one ref:
git fetch --depth=1 origin <base-sha>

How to prevent it

  • Use fetch-depth: 0 for jobs that diff against a base branch, compute changed files, or read tags; reserve shallow clones for jobs that only build the tip.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →