rust-lang/cargo · error

unrecognized `git --version` output: {stdout}

Error message

unrecognized `git --version` output: {stdout}

What it means

Cargo parses the output of `git --version` in `GitVersion::from_version_stdout` to determine the installed git version. If stdout does not start with the expected `git version ` prefix (matching git's own help.c format), the parse fails and this error is thrown. It means the `git` binary on PATH produced unexpected output — often not a real git, or a wrapper/shim emitting extra text.

Source

Thrown at src/sources/git/utils.rs:1175

    }

    static CACHE: std::sync::OnceLock<Option<GitVersion>> = std::sync::OnceLock::new();
    *CACHE.get_or_init(git_version)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[allow(unused)]
struct GitVersion {
    major: usize,
    minor: usize,
    patch: usize,
}

impl GitVersion {
    fn from_version_stdout(stdout: &str) -> Result<Self, anyhow::Error> {
        // See https://github.com/git/git/blob/f78ce2f7b6df702f93d40b85d6bda92a3f65da79/help.c#L779-L784
        let Some(version) = stdout.strip_prefix("git version ") else {
            anyhow::bail!("unrecognized `git --version` output: {stdout}")
        };
        let (version, _) = version.split_once(" ").unwrap_or((version, ""));
        let (version, _) = version.split_once("\n").unwrap_or((version, ""));
        version.parse()
    }
}

impl std::str::FromStr for GitVersion {
    type Err = anyhow::Error;

    fn from_str(version: &str) -> Result<Self, Self::Err> {
        let unreleased = "GIT";

        let s = version;
        let (major, s) = s.split_once(".").unwrap_or((s, ""));
        let mut major: usize = major.parse().map_err(|_err| {
            anyhow::format_err!("unrecognized major version `{major}` in `{version}`")
        })?;

View on GitHub (pinned to 42eee92bc9)

Solutions

  1. Run `git --version` manually and confirm output starts with `git version `; fix or remove any wrapper/shim shadowing the real git on PATH.
  2. Reinstall or repair git so `git --version` prints the standard format (e.g. `git version 2.43.0`).
  3. Check PATH for unexpected git entries (`which -a git`) and remove the offending one.
  4. If using cargo's git CLI fetching, set `net.git-fetch-with-cli = false` to avoid the version check path where applicable.

Example fix

// before: custom wrapper prints banner
#!/bin/sh
echo "ACME git wrapper"
exec /usr/bin/git "$@"
// after: pass version query through untouched
#!/bin/sh
if [ "$1" = "--version" ]; then exec /usr/bin/git --version; fi
exec /usr/bin/git "$@"
Defensive patterns

Strategy: validation

Validate before calling

let out = std::process::Command::new("git").arg("--version").output()?;
let stdout = String::from_utf8_lossy(&out.stdout);
if !stdout.starts_with("git version ") {
    eprintln!("git on PATH is not a real git (output: {stdout:?}); fix PATH/reinstall git");
}

Prevention

When it happens

Trigger: Running `git --version` returns stdout that does not begin with `git version ` (after any CLI/shell wrapping); e.g. a shim script, a git alias binary, or an environment where git prints a warning/banner before or instead of the version line.

Common situations: Developers with a `git` wrapper script (e.g. for credential handling) that rewrites output; Windows/MSYS environments where PATH resolves to a non-git stub; corporate sandbox wrappers; corrupted or replaced git installations.

Related errors


AI-assisted analysis of rust-lang/cargo@42eee92bc9 (2026-09-08). Data as JSON: /api/errors/25ff49a19971b989. Report an issue: GitHub.