nikivdev/code · error

git {} failed

Error message

git {} failed

What it means

Raised by git_capture_in in src/push.rs when a git command whose stdout the tool captures (e.g. `git remote get-url ...`) exits with a non-zero status. The message includes the full git argument list so the failing subcommand is identifiable; stderr is not shown, only the failure indicator.

Source

Thrown at src/push.rs:355

        .context("failed to read current branch")?;
    if !output.status.success() {
        bail!("git rev-parse --abbrev-ref HEAD failed");
    }
    let name = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if name.is_empty() || name == "HEAD" {
        bail!("detached HEAD (checkout a branch first)");
    }
    Ok(name)
}

pub(crate) fn git_capture_in(repo_root: &Path, args: &[&str]) -> Result<String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(repo_root)
        .output()
        .with_context(|| format!("failed to run git {}", args.join(" ")))?;
    if !output.status.success() {
        bail!("git {} failed", args.join(" "));
    }
    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

pub(crate) fn git_run_in(repo_root: &Path, args: &[&str]) -> Result<()> {
    git_run_in_with_env(repo_root, args, &[])
}

pub(crate) fn git_run_in_with_env(
    repo_root: &Path,
    args: &[&str],
    envs: &[(&str, &str)],
) -> Result<()> {
    let status = Command::new("git")
        .args(args)
        .current_dir(repo_root)
        .envs(envs.iter().copied())
        .stdin(Stdio::inherit())

View on GitHub (pinned to a747e741ae)

Solutions

  1. Read the error text to identify the exact failing git subcommand and re-run it manually in the repo to see stderr details
  2. Fix the underlying issue (create the missing remote/branch, fix credentials, check network)
  3. Ensure the command is run from a valid repository root

Example fix

// before: branch does not exist
bail!("git ls-remote ... failed")
// after: verify the ref exists first
let out = git_capture_in(root, &["rev-parse", "--verify", branch])?;
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure refs/remotes exist before capture
let ok = std::process::Command::new("git")
    .args(["rev-parse", "--verify", ref_name])
    .output()?
    .status.success();
if !ok { eprintln!("ref {ref_name} does not exist"); }

Try / catch

match result {
    Err(e) if e.to_string().starts_with("git ") && e.to_string().ends_with(" failed") => {
        // rerun the same args manually with stderr visible to get the cause
    }
    Err(e) => return Err(e),
    Ok(v) => Ok(v),
}

Prevention

When it happens

Trigger: Any captured git invocation fails — most commonly `git remote get-url <remote>` for a remote that does not exist (handled elsewhere as Ok), but also `git ls-remote`/similar failing due to network/auth, invalid args, or a bad repo state under repo_root.

Common situations: Typo'd remote or branch names; pushing to a repo the user cannot access (auth failure); network offline; running from a worktree path that no longer exists.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/57645d14a995a4c8. Report an issue: GitHub.