git-ecosystem/git-credential-manager · error · Trace2Exception

Failed to add Git configuration entry

Error message

Failed to add Git configuration entry '{name}'

What it means

Thrown by GitConfiguration.Add when `git config --add <name> <value>` exits non-zero. Add appends a multi-valued entry; failure usually means the key name is invalid or the config file cannot be written.

Solutions

  1. Use a valid 'section.key' (or 'section.subsection.key') name
  2. Ensure you are in a valid repository when using the Local level and the file is writable
  3. Run `git config --add <level> <name> <value>` manually to reproduce and see git's stderr
  4. Check for file locks or read-only attributes on the config file

Example fix

// before
config.Add(level, "fetchmirror", "..."); // throws: no section
// after
config.Add(level, "remote.origin.mirror", "true"); // valid key
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidConfigKey(string name) {
    var parts = name.Split('.');
    return parts.Length >= 2 && parts.All(p => p.Length > 0);
}
// plus: ensure working dir is a repository when level == Local

Try / catch

try { cfg.Add(level, name, value); }
catch (GitException ex) { log.Error($"Add '{name}' failed: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling Add(level, name, value) with a malformed key name, or when the underlying `git config --add` fails because the config file is read-only, locked, or the level's file cannot be created.

Common situations: Adding to a local config in a non-repository directory; invalid key like 'remoteorigin' without dots; enterprise environments where config files are managed read-only by policy.

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 git-ecosystem/git-credential-manager@e8ce762cd0 (2026-09-11). Data as JSON: /api/errors/157871705ebe917b. Report an issue: GitHub.

Appendix: source

Thrown at src/Core/GitConfiguration.cs:620

        public void Add(GitConfigurationLevel level, string name, string value)
        {
            EnsureSpecificLevel(level);

            string levelArg = GetLevelFilterArg(level);
            using (ChildProcess git = _git.CreateProcess($"config {levelArg} --add {QuoteCmdArg(name)} {QuoteCmdArg(value)}"))
            {
                git.Start(Trace2ProcessClass.Git);
                git.WaitForExit();

                switch (git.ExitCode)
                {
                    case 0: // OK
                        InvalidateCache();
                        break;
                    default:
                        _trace.WriteLine($"Failed to add config entry '{name}' with value '{value}' (exit={git.ExitCode}, level={level})");
                        throw GitProcess.CreateGitException(git, $"Failed to add Git configuration entry '{name}'");
                }
            }
        }

        public void Unset(GitConfigurationLevel level, string name)
        {
            EnsureSpecificLevel(level);

            string levelArg = GetLevelFilterArg(level);
            using (ChildProcess git = _git.CreateProcess($"config {levelArg} --unset {QuoteCmdArg(name)}"))
            {
                git.Start(Trace2ProcessClass.Git);
                git.WaitForExit();

                switch (git.ExitCode)
                {
                    case 0: // OK
                    case 5: // Trying to unset a value that does not exist

View on GitHub (pinned to e8ce762cd0)