Skip to content
Latchkey

Git "fatal: bad object" in CI

Git was asked about an object - a commit, tag, or ref - that is not in the local object store. Most often the workspace is a shallow clone and the SHA you referenced was never fetched, so Git cannot resolve it.

What this error means

A command like git diff, git merge-base, or git rev-parse fails with fatal: bad object <ref>. A full clone of the same repo resolves the object fine; the shallow CI checkout does not.

git output
$ git merge-base origin/main HEAD
fatal: bad object origin/main

Common causes

The object is below the shallow depth

A fetch-depth: 1 checkout has only the tip. A base-branch ref or older SHA you reference is simply not present, so Git reports a bad object.

The ref was never fetched

A single-branch clone has no origin/main locally, or a tag was never fetched, leaving the name unresolvable.

How to fix it

Fetch the ref you reference

Pull the missing branch/object so Git can resolve it.

Terminal
git fetch --no-tags --depth=1 origin main:refs/remotes/origin/main
git merge-base origin/main HEAD

Fetch full history in the checkout

For jobs that diff against a base or walk history, fetch everything.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0

How to prevent it

  • Fetch the base branch explicitly before any diff/merge-base step.
  • Use fetch-depth: 0 for history-dependent jobs.
  • Validate refs with git rev-parse --verify before using them in scripts.

Related guides

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