reviewdog: error: fail to get git diff: ambiguous argument 'origin/main...HEAD'
Fixes reviewdog's 'fail to get git diff: ambiguous argument' failure in CI. Use when the reviewdog step dies because the base ref (origin/main) was never fetched, typically from a shallow or single-branch checkout. Covers fetch-depth fixes and explicit base-ref fetching. Not for diff parse errors, annotation failures, or linter findings.
TL;DR
The base ref does not exist in the CI checkout. reviewdog asks git for the diff against origin/main, but the runner never fetched that ref, which is the normal result of a shallow or single-branch clone. Fetch the full history (or at least the base branch) before the diff step and the error goes away. This is a checkout problem, not a reviewdog problem.
The error
reviewdog: error: fail to get git diff: ambiguous argument 'origin/main...HEAD'Steps
1. Confirm the base ref is missing
git rev-parse --verify origin/mainExpected output right now: a fatal "unknown revision" message. That confirms the diagnosis. After the fix, this command prints a commit SHA.
2. Inspect the checkout step
Open the workflow and find the checkout step. Look for fetch-depth: 1 (the default) or a missing fetch-depth key, which means shallow, single-branch history. Expected output: you can see exactly which step produces the broken checkout.
3. Fetch the full history
Change the checkout step to fetch everything:
- uses: actions/checkout@v4
with:
fetch-depth: 0Expected output: the next run fetches all branches and tags, and the verify command from step 1 prints a SHA.
4. Or fetch just the base ref
If a full clone is too slow for a big repo, keep the shallow clone and add an explicit fetch before reviewdog runs:
git fetch origin main:refs/remotes/origin/mainExpected output: the ref is created locally without downloading the whole history.
5. Match the real default branch name
If the repo's default branch is master or develop instead of main, use that name in the fetch and in the diff range. Check the repo settings for the default branch name. Expected output: the diff range resolves against a ref that actually exists.
6. Re-run the reviewdog step
Push the workflow change and re-run the failed job. Expected output: reviewdog exits 0 and posts annotations instead of the git error.
When to use
- The reviewdog CI step fails with "ambiguous argument" on the base ref
- The workflow uses the default shallow checkout
- PRs from forks fail while same-repo PRs pass
When not to use
- reviewdog diff parse errors (malformed hunks, binary patches)
- Annotation posting failures (permissions, line mapping)
- The ref exists locally but the diff is empty
Tool compatibility
- reviewdog v0.14 and later, all reporters
- actions/checkout v3 and v4 (fetch-depth behaves the same)
- git 2.25 and later (refspec fetch syntax)
Variant phrasings
reviewdog ambiguous argument origin/master...HEAD
Same fix. Swap in whatever the default branch is called. Repos created before late 2020 usually default to master.
reviewdog fails on pullrequesttarget with missing base
pullrequesttarget checks out the base repo, but a shallow fetch still skips the base ref. The explicit fetch in step 4 works here too.
git diff ambiguous argument in GitHub Actions
This is the same underlying git behavior outside reviewdog. Any tool that diffs against a remote ref needs that ref fetched first.
Why it happens
The triple-dot range origin/main...HEAD needs both endpoints to exist locally. A shallow checkout only downloads the PR head commit, so origin/main is unknown and git cannot compute the merge base. reviewdog surfaces git's complaint verbatim.
Edge cases
- Force-pushed PRs: the fetched base is fine, but a stale PR head can still confuse the diff. Re-fetch the PR ref too.
- Very large repos: fetch-depth 0 can add minutes. Prefer the targeted single-ref fetch in step 4.
- Tags-only workflows: if the diff range uses a tag, fetch tags explicitly with a tag refspec.
- Monorepos with sparse checkout: the ref problem is independent of sparse paths, but confirm the sparse config still includes the files reviewdog needs.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_3pOf4oaIxRnQMNcuJKVvBg