nikivdev/code · error

git {} failed

Error message

git {} failed

What it means

src/config.rs runs an external `git` command (with cwd = repo_root) and, if the child process exits with a non-zero status, bails with `git <args> failed`. The library throws this because the requested git operation (status, rev-parse, diff, etc.) did not succeed — e.g. the directory is not a git repository, a ref/branch doesn't exist, or the working tree has a problem git refuses to proceed on. Note the error message itself does not carry git's stderr; the underlying cause must be inspected separately.

Source

Thrown at src/config.rs:2905

        .map(|branch| branch.trim().to_string())
        .filter(|branch| !branch.is_empty() && branch != "HEAD")
}

fn git_config_get(repo_root: &Path, key: &str) -> Option<String> {
    git_capture_in(repo_root, &["config", "--get", key])
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
}

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() {
        anyhow::bail!("git {} failed", args.join(" "));
    }
    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

/// Load config from the given path, logging a warning and returning an empty
/// config if anything goes wrong. This keeps the daemon usable even if the
/// config file is missing or invalid.
pub fn load_or_default<P: AsRef<Path>>(path: P) -> Config {
    match load(path) {
        Ok(cfg) => cfg,
        Err(err) => {
            tracing::warn!(
                ?err,
                "failed to load flow config; starting with no managed servers"
            );
            Config::default()
        }
    }

View on GitHub (pinned to a747e741ae)

Solutions

  1. Run the same git command manually in the repo root to see the real stderr (the anyhow message omits it).
  2. Verify the directory is a git repository: `git -C <repo_root> rev-parse --is-inside-work-tree`.
  3. Remove a stale `.git/index.lock` if git reports an index lock.
  4. Confirm the referenced branch/tag/ref exists (`git branch -a`, `git tag`), or use a ref that does.
  5. Set git identity/credentials if the command requires them (`git config user.email/user.name`).

Example fix

// before: context lost, only args shown
anyhow::bail!("git {} failed", args.join(" "));
// after: include stderr for debuggability
anyhow::bail!("git {} failed: {}", args.join(" "), String::from_utf8_lossy(&output.stderr));
Defensive patterns

Strategy: try-catch

Validate before calling

// Rust: pre-check repo before calling the config helper
let out = std::process::Command::new("git")
    .args(["rev-parse", "--is-inside-work-tree"])
    .current_dir(repo_root)
    .output()?;
if !out.status.success() {
    anyhow::bail!("{} is not a git repository", repo_root.display());
}

Try / catch

match load_config_from_git(repo_root) {
    Ok(cfg) => use(cfg),
    Err(e) if e.to_string().starts_with("git ") => {
        eprintln!("git failed: {e:#}. Check `git status` in {:?} and that the ref exists.", repo_root);
        fallback_to_default_config()
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Any call into this config helper that shells out to `git <args>` inside repo_root where git exits non-zero: running git commands in a directory that is not a git repo, referencing a nonexistent branch/tag/commit, or git failing due to index lock, bad config, or no user identity configured for commands that need one.

Common situations: Reading config from a project root that was never `git init`-ed; querying a commit hash on a detached/renamed branch; a stale `.git/index.lock` left by a crashed git process; running in CI with a shallow clone where history is missing; missing gitcredentials for remote operations.

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/a1b41a927bdab75a. Report an issue: GitHub.