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 merge-base origin/main HEAD
fatal: bad object origin/mainCommon 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.
git fetch --no-tags --depth=1 origin main:refs/remotes/origin/main
git merge-base origin/main HEADFetch full history in the checkout
For jobs that diff against a base or walk history, fetch everything.
- uses: actions/checkout@v4
with:
fetch-depth: 0How to prevent it
- Fetch the base branch explicitly before any diff/merge-base step.
- Use
fetch-depth: 0for history-dependent jobs. - Validate refs with
git rev-parse --verifybefore using them in scripts.