Skip to content
Latchkey

Nx affected "Unable to find base in git history" in CI

nx affected compares your branch against a base SHA to decide which projects changed. In CI the clone is often shallow, so the base commit is not in the local history and Nx cannot compute the diff.

What this error means

nx affected fails complaining it cannot find the base or head SHA in git history, or that the commit is missing. It typically passes locally (full clone) but fails in CI where actions/checkout defaults to depth 1.

Nx output
NX   Unable to find base "origin/main" in the git history.

 Please ensure to checkout the branch / commit you are comparing against,
 or fetch enough history with fetch-depth: 0.

Common causes

Shallow clone in CI

CI checkouts default to a depth of 1, so only the tip commit exists locally. The base commit nx affected needs to diff against is simply not in the repository.

Base branch not fetched

Even with more depth, the comparison branch (e.g. origin/main) may not be fetched, so the ref does not resolve.

How to fix it

Fetch full history in checkout

Set fetch-depth: 0 so the base commit and branch are present for the diff.

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

Use the Nx SHAs helper for PRs

The official action derives correct base/head SHAs for pushes and pull requests.

.github/workflows/ci.yml
- uses: nrwl/nx-set-shas@v4
- run: nx affected -t build --base=${{ env.NX_BASE }} --head=${{ env.NX_HEAD }}

Or pass an explicit, present base

When you control the range, point --base/--head at commits you know are in the checkout.

Terminal
git fetch origin main --depth=50
nx affected -t test --base=origin/main --head=HEAD

How to prevent it

  • Use fetch-depth: 0 (or nrwl/nx-set-shas) on any job that runs nx affected.
  • Fetch the base branch explicitly before computing affected projects.
  • Avoid relying on the default shallow checkout for diff-based commands.

Frequently asked questions

What causes ""Unable to find base ... in git history""?
CI checkouts default to a depth of 1, so only the tip commit exists locally. The base commit nx affected needs to diff against is simply not in the repository.
How do I fix "Unable to find base ... in git history"?
Set fetch-depth: 0 so the base commit and branch are present for the diff.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card