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 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.
- uses: actions/checkout@v4
with:
fetch-depth: 0Use the Nx SHAs helper for PRs
The official action derives correct base/head SHAs for pushes and pull requests.
- 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.
git fetch origin main --depth=50
nx affected -t test --base=origin/main --head=HEADHow to prevent it
- Use
fetch-depth: 0(ornrwl/nx-set-shas) on any job that runsnx affected. - Fetch the base branch explicitly before computing affected projects.
- Avoid relying on the default shallow checkout for diff-based commands.