nikivdev/code · error · anyhow::Error

git config --global --add {} failed

Error message

git config --global --add {} failed

What it means

git_config_add runs `git config --global --add <key> <value>` (used by add_url_rewrite to append e.g. insteadOf/url rewrite entries without replacing existing values) and bails if git exits nonzero. The multi-valued config entry was not appended.

Source

Thrown at src/ssh.rs:472

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

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

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

    Ok(())
}

View on GitHub (pinned to a747e741ae)

Solutions

  1. Run the exact `git config --global --add <key> <value>` manually to see git's stderr
  2. Remove stale ~/.gitconfig.lock and ensure ~/.gitconfig parses (`git config --global --list`)
  3. Confirm HOME is set and writable in the execution environment
  4. Apply the rewrite entry manually in ~/.gitconfig if automation keeps failing

Example fix

// before
anyhow::bail!("git config --global --add {} failed", key);
// after: surface git diagnostics
anyhow::bail!("git config --global --add {} failed: {}", key, String::from_utf8_lossy(&output.stderr));
Defensive patterns

Strategy: try-catch

Validate before calling

// verify global config is writable and not locked before --add
let home = std::env::var_os("HOME").map(std::path::PathBuf::from);
if home.map(|h| h.join(".gitconfig.lock").exists()).unwrap_or(false) {
    eprintln!("stale ~/.gitconfig.lock present; git config --add will fail");
}

Try / catch

match add_url_rewrite(key, desired) {
    Ok(changed) => { if changed { println!("rewrite added"); } }
    Err(e) if e.to_string().contains("git config --global --add") => {
        eprintln!("failed to append rewrite: {e:#}; add it manually to ~/.gitconfig");
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: add_url_rewrite calls git_config_add while the global config is unwritable, corrupt, HOME is unset/misconfigured, or a config.lock is present — git exits nonzero and the add is lost.

Common situations: CI container without writable HOME; ~/.gitconfig.lock left by a crashed git; malformed include directives breaking config parsing; read-only filesystem.

Related errors


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