nikivdev/code · error

git {} failed

Error message

git {} failed

What it means

git_capture_in runs an arbitrary git subcommand inside repo_root and captures stdout; if git exits non-zero, it bails with 'git <args> failed'. The error is intentionally generic — git's own stderr is shown above it — and covers any failing git call (status, branch, log, remote) used to build the repo snapshot.

Source

Thrown at src/publish.rs:725

            .context("failed to stage files")?;
        Command::new("git")
            .args(["commit", "-m", "Initial commit"])
            .current_dir(repo_root)
            .status()
            .context("failed to create initial commit")?;
    }

    Ok(())
}

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())
}

fn build_repo_snapshot(
    repo_root: &Path,
    default_branch: &str,
    description: Option<String>,
) -> Result<RepoSnapshot> {
    let tree_output = git_capture_in(repo_root, &["ls-tree", "-r", "-t", "-l", "HEAD"])?;
    let mut tree = Vec::new();
    let mut files = Vec::new();
    let mut seen_paths = HashSet::new();
    let mut total_bytes: u64 = 0;
    let mut skipped_files: usize = 0;

    let mut readme_path: Option<String> = None;

View on GitHub (pinned to a747e741ae)

Solutions

  1. Read the git stderr printed above the error for the exact failing subcommand
  2. If it's 'dubious ownership', add `git config --global --add safe.directory <path>`
  3. If it's no-HEAD errors, make an initial commit before publishing
  4. Verify remotes: `git remote -v`, and fix or remove broken remote entries

Example fix

// before (fresh repo, no commits)
f publish -y  ->  git log failed
// after
git add -A && git commit -m "initial commit"
f publish -y
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight the git commands that feed the snapshot
let head = std::process::Command::new("git").args(["rev-parse","HEAD"]).output()?;
if !head.status.success() {
    eprintln!("repo has no commits; make an initial commit before publishing");
    std::process::exit(1);
}

Try / catch

if let Err(e) = run_gitedit(&opts) {
    let msg = e.to_string();
    if msg.starts_with("git ") && msg.ends_with(" failed") {
        eprintln!("git subcommand failed: {} — check stderr above (ownership? no commits? broken remote?)", msg);
    } else { return Err(e); }
}

Prevention

When it happens

Trigger: Any captured git command exits non-zero: `git status` on a repo with a corrupt index, `git log` before the first commit (no HEAD), `git remote get-url` for a nonexistent remote, or git refusing to operate due to dubious ownership (`safe.directory`).

Common situations: Brand-new repo with zero commits (rev-parse HEAD fails); repos mounted into containers owned by a different UID triggering git's 'dubious ownership' protection; deleted/moved remotes referenced in config.

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