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

Thrown as `GitNotFoundError` (code `VITEST_GIT_NOT_FOUND`) from `GitVCSProvider.findChangedFiles` (git.ts:31) when `getRoot(cwd)` returns null, meaning `git rev-parse --show-cdup` failed or returned nothing. This provider powers the `--changed` / `-w` (watch changed files) features; without a git repository root it cannot compute changed files, so it aborts with a hint to initialize git.

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 d568f8ce37)

Solutions

  1. Run `git init` (and make at least one commit) in the project root.
  2. Ensure `git` is installed and on PATH in the running environment.
  3. Avoid `--changed`/`-w` flags in environments without a git work tree; run the full suite instead.
  4. If in CI, make sure the checkout step preserves the `.git` directory.

Example fix

# before
vitest --changed   # in a non-git directory
# after
git init && git add -A && git commit -m init
vitest --changed
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync } from 'node:fs'
import { resolve } from 'node:path'
let dir = process.cwd()
while (dir !== resolve(dir, '..')) {
  if (existsSync(resolve(dir, '.git'))) break
  dir = resolve(dir, '..')
}
if (!existsSync(resolve(dir, '.git'))) throw new Error('Not inside a git work tree')

Prevention

When it happens

Trigger: Running `vitest --changed` or `vitest -w` inside a directory that is not inside any git work tree; running in a fresh project before `git init`; running in a container/CI checkout where `.git` was stripped out; git not installed so `x('git', ...)` throws and `getRoot` swallows it returning null.

Common situations: New project not yet `git init`'d; archived/extracted source without `.git`; minimal Docker images lacking git; CI that does a shallow copy without the `.git` directory.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/0b6539a97e39783c.json. Report an issue: GitHub.