Skip to content

GitHub Actions CI fails: Cannot find 'main' branch for git diff (fatal: ambiguous argument 'main...HEAD') #3

Description

@AbraaoAlves

Problem

When running the codeguardian checks in a GitHub Actions CI environment, the job fails with the following error:

Error: fatal: ambiguous argument 'main...HEAD': unknown revision or path not in the working tree.

This breaks CI runs for projects that rely on GitHub-hosted runners and default checkout strategies.

Root Cause

  • The CLI and npm scripts set main as the default base branch for git comparisons (e.g., --base main or -b main).
  • In a typical GitHub Actions workflow, the local clone does not guarantee the presence of the main branch ref by default, especially if actions/checkout uses a shallow fetch (default fetch-depth: 1).
  • As a result, any git diff main...HEAD or similar operation fails in CI, even though it works locally.

Evidence

  • Full error: fatal: ambiguous argument 'main...HEAD': unknown revision or path not in the working tree.
  • Scripts in package.json enforce -b main by default.

Suggested Solutions

  • Make the CLI and workflow scripts resolve the actual default branch robustly, not just assume main exists.
  • In CI, prefer to use origin/<default branch> or allow an override based on the GitHub event context (github.event.pull_request.base.ref).
  • Add fallback logic in the CLI for when main isn't available as a local ref.
  • Recommend in the README (and optionally in workflow examples) that users set fetch-depth: 0 in actions/checkout or otherwise ensure the base branch is locally present.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions