golang/go · error

failed to execute git version: %w

Error message

failed to execute git version: %w

What it means

gitVersion() shells out to `git version` via exec.Command and this error wraps the exec failure with %w. It means the go command could not even invoke the git binary. The function returns "v0" as a fallback version alongside the error.

Source

Thrown at src/cmd/go/internal/modfetch/codehost/git.go:995

}

func (r *gitRepo) runGit(ctx context.Context, cmdline ...any) ([]byte, error) {
	args := RunArgs{cmdline: cmdline, dir: r.dir, local: r.local}
	if !r.local {
		// Manually supply GIT_DIR so Git works with safe.bareRepository=explicit set.
		// This is necessary only for remote repositories as they are initialized with git init --bare.
		args.env = []string{"GIT_DIR=" + r.dir}
	}
	return RunWithArgs(ctx, args)
}

// Capture the major, minor and (optionally) patch version, but ignore anything later
var gitVersLineExtract = regexp.MustCompile(`git version\s+(\d+\.\d+(?:\.\d+)?)`)

func gitVersion() (string, error) {
	gitOut, runErr := exec.Command("git", "version").CombinedOutput()
	if runErr != nil {
		return "v0", fmt.Errorf("failed to execute git version: %w", runErr)
	}
	return extractGitVersion(gitOut)
}

func extractGitVersion(gitOut []byte) (string, error) {
	matches := gitVersLineExtract.FindSubmatch(gitOut)
	if len(matches) < 2 {
		return "v0", fmt.Errorf("git version extraction regexp did not match version line: %q", gitOut)
	}
	return "v" + string(matches[1]), nil
}

func hasAtLeastGitVersion(minVers string) (bool, error) {
	gitVers, gitVersErr := gitVersion()
	if gitVersErr != nil {
		return false, gitVersErr
	}
	return semver.Compare(minVers, gitVers) <= 0, nil

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Install git in the environment (apt-get install git, apk add git, choco install git, etc.).
  2. Ensure the git executable is on PATH for the go command process — print `os.Getenv("PATH")` from the launcher to confirm.
  3. On Windows verify git.exe is reachable; the go command looks for bare `git`.
  4. If git truly cannot be provided, switch the dependency to a proxy-served module (GOPROXY) so the codehost git path is never exercised.

Example fix

// before — distroless image with no git
FROM gcr.io/distroless/static
// error: failed to execute git version: exec: "git": executable file not found in $PATH

// after
FROM debian:slim
RUN apt-get update && apt-get install -y git
Defensive patterns

Strategy: validation

Validate before calling

func gitAvailable() bool {
    _, err := exec.LookPath("git")
    return err == nil
}

// call before invoking any go mod subcommand that may touch a VCS.

Try / catch

if vers, err := gitVersion(); err != nil {
    if strings.Contains(err.Error(), "failed to execute git version") {
        // surface a friendlier 'install git' message to the user
    }
    return "v0", err
}

Prevention

When it happens

Trigger: Any code path that calls hasAtLeastGitVersion or gitVersion when git is not on PATH, not executable, or the process lacks permission to spawn it.

Common situations: Minimal containers (distroless, scratch-derived) that omit git; PATH overwritten by a wrapper script; git binary renamed on Windows; CI images that install git only in a later stage.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/f20245bfe73835eb. Report an issue: GitHub.