vitest-dev/vitest · error · GitNotFoundError

VITEST_GIT_NOT_FOUND

VITEST_GIT_NOT_FOUND

Error message

Could not find Git root. Have you initialized git with `git init`?

What it means

`--changed`/`--changedSince` rely on git to diff changed files. `GitVCSProvider.getRoot` runs `git rev-parse --show-cdup`; if that fails (not a git repository, or `git` not installed/on PATH) it returns null, and `findChangedFiles` then throws `GitNotFoundError` (`VITEST_GIT_NOT_FOUND`).

Solutions

  1. Initialize the repository with `git init` and make at least one commit.
  2. Ensure the `git` binary is installed and on `PATH` (`git --version` should succeed in the same environment).
  3. Drop `--changed`/`--changedSince` (and the `changed` config) if the project intentionally doesn't use git.

Example fix

# before
vitest --changed   # VITEST_GIT_NOT_FOUND

# after
git init && git add -A && git commit -m 'init'
vitest --changed
Defensive patterns

Strategy: try-catch

Validate before calling

import { x } from 'tinyexec'
async function isGitRepo(cwd: string): Promise<boolean> {
  try {
    await x('git', ['rev-parse', '--show-cdup'], { nodeOptions: { cwd } })
    return true
  } catch {
    return false
  }
}
// before calling vitest with --changed:
if (options.changed && !(await isGitRepo(process.cwd()))) {
  throw new Error('--changed requires a git repository. Run `git init` or drop the flag.')
}

Try / catch

import { GitNotFoundError } from 'vitest/node'
try {
  await startVitest('test', [], { changed: 'HEAD~1' }, {})
} catch (e) {
  if (e instanceof GitNotFoundError) {
    // not a git repo: fall back to running the full suite
    return startVitest('test', [], {}, {})
  }
  throw e
}

Prevention

When it happens

Trigger: Invoking `vitest --changed` (or `--changedSince <ref>`, or the `changed`/`changedSince` config option) in a directory where `git rev-parse --show-cdup` fails — no `.git`, or `git` binary missing.

Common situations: Fresh/uninitialized project; CI checkout that strips `.git`; minimal container without git installed; running inside a sandbox that hides git.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/0b6539a97e39783c. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/node/vcs/git.ts:31

    try {
      result = await x('git', args, { nodeOptions: { cwd: this.root } })
    }
    catch (e: any) {
      e.message = e.stderr

      throw e
    }

    return result.stdout
      .split('\n')
      .filter(s => s !== '')
      .map(changedPath => resolve(this.root, changedPath))
  }

  async findChangedFiles(options: VCSProviderOptions): Promise<string[]> {
    const root = this.root || await this.getRoot(options.root)
    if (!root) {
      throw new GitNotFoundError()
    }

    this.root = root

    const changedSince = options.changedSince
    if (typeof changedSince === 'string') {
      const [committed, staged, unstaged] = await Promise.all([
        this.getFilesSince(changedSince),
        this.getStagedFiles(),
        this.getUnstagedFiles(),
      ])
      return [...committed, ...staged, ...unstaged]
    }
    const [staged, unstaged] = await Promise.all([
      this.getStagedFiles(),
      this.getUnstagedFiles(),
    ])
    return [...staged, ...unstaged]

View on GitHub (pinned to 1fa9837ec2)