nikivdev/code · error · anyhow::Error

git config --global {} failed

Error message

git config --global {} failed

What it means

git_config_set runs `git config --global <key> <value>` and bails with this message if git exits nonzero. It means a global git configuration write failed — the key/value never landed in ~/.gitconfig, so dependent behavior (e.g. GIT_SSH_COMMAND setup) will not take effect.

Source

Thrown at src/ssh.rs:459

        return Ok(Vec::new());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    Ok(stdout
        .lines()
        .map(|line| line.trim().to_string())
        .filter(|line| !line.is_empty())
        .collect())
}

fn git_config_set(key: &str, value: &str) -> Result<()> {
    let status = Command::new("git")
        .args(["config", "--global", key, value])
        .status()
        .context("failed to run git config")?;

    if !status.success() {
        anyhow::bail!("git config --global {} failed", key);
    }

    Ok(())
}

fn git_config_add(key: &str, value: &str) -> Result<()> {
    let status = Command::new("git")
        .args(["config", "--global", "--add", key, value])
        .status()
        .context("failed to run git config")?;

    if !status.success() {
        anyhow::bail!("git config --global --add {} failed", key);
    }

    Ok(())
}

View on GitHub (pinned to a747e741ae)

Solutions

  1. Run `git config --global <key> <value>` by hand to see git's real stderr
  2. Verify HOME is set and writable (echo $HOME; touch ~/.gitconfig)
  3. Fix or remove a corrupt ~/.gitconfig (git config --global --list to test parsing)
  4. Check for a concurrent git process holding a config.lock and remove stale ~/.gitconfig.lock

Example fix

// before: key only, no git stderr
anyhow::bail!("git config --global {} failed", key);
// after: capture git's diagnostic output
anyhow::bail!("git config --global {} failed: {}", key, String::from_utf8_lossy(&output.stderr));
Defensive patterns

Strategy: try-catch

Validate before calling

fn can_write_global_gitconfig() -> bool {
    std::env::var_os("HOME").map(|h| {
        let p = std::path::PathBuf::from(h).join(".gitconfig");
        match p.exists() {
            true => std::fs::OpenOptions::new().append(true).open(&p).is_ok(),
            false => std::fs::OpenOptions::new().write(true).create_new(true).open(&p).is_ok(),
        }
    }).unwrap_or(false)
}
if !can_write_global_gitconfig() { eprintln!("global git config not writable; fix HOME/permissions first"); }

Try / catch

match ensure_git_ssh_command() {
    Ok(()) => proceed(),
    Err(e) if e.to_string().contains("git config --global") => {
        eprintln!("could not write global git config: {e:#}; set GIT_SSH_COMMAND env var manually instead");
        std::env::set_var("GIT_SSH_COMMAND", ssh_command);
        proceed();
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: ensure_git_ssh_command / ensure_git_ssh_command_for_sock / ensure_git_ssh_command_wrapper call git_config_set while git cannot write the global config: unwritable or corrupt ~/.gitconfig, HOME unset or pointing somewhere unwritable, include.path loops, or a broken git installation.

Common situations: HOME not set in CI containers so git can't resolve the global config path; ~/.gitconfig owned by root or locked by a concurrent git process; malformed config file causing git to abort on any config write; read-only home directory.

Related errors


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