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

Failed to get current Git repository

Error message

Failed to get current Git repository

What it means

Thrown by Git.GetCurrentRepositoryInternal when the `git rev-parse --show-toplevel` (or similar) process exits with an unexpected non-zero, non-128 exit code. Exit code 128 (not a repository) is handled by returning null, so this error means git failed for some other reason — a corrupted repo, a broken git installation, or an unreadable working directory. It wraps the underlying git stderr via CreateGitException.

Solutions

  1. Run `git rev-parse --show-toplevel` manually in the same directory to see the real git error
  2. Verify the repository is not corrupted (`git status`, `git fsck`) and restore .git if needed
  3. Check the git binary works (`git --version`) and PATH points to a valid installation
  4. Check file/directory permissions on the working tree and .git folder

Example fix

// before
var repo = git.GetCurrentRepository(); // throws on exit=129
// after
if (!git.IsInsideRepository(path)) return;
try { var repo = git.GetCurrentRepository(); }
catch (GitException ex) { _trace.WriteLine(ex.Message); }
Defensive patterns

Strategy: try-catch

Validate before calling

// before calling
directory.CreateDirectoryIfNeeded();
if (!System.IO.Directory.Exists(workingDir)) return null;
var probe = Process.Run("git", "rev-parse --git-dir", workingDir);
if (probe.ExitCode != 0) return null;

Type guard

bool IsUsableRepo(Git git) { try { return git.IsInsideRepository(); } catch (GitException) { return false; } }

Try / catch

try { var repo = git.GetCurrentRepository(); }
catch (GitException ex) { log.Warn($"Repo detection failed: {ex.Message}"); repo = null; }

Prevention

When it happens

Trigger: Calling GetCurrentRepository or IsInsideRepository when the git subprocess exits with a code other than 0 or 128 — e.g. corrupted .git directory, git not functioning, permission errors reading the repo, or a disk/IO failure during rev-parse.

Common situations: Running inside a partially deleted or corrupted repository; a broken or very old git binary; antivirus or indexing locking .git files; running with insufficient permissions on the working tree.

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

Appendix: source

Thrown at src/Core/Git.cs:170

                if (suppressStreams)
                {
                    git.Process.ErrorDataReceived += (_, _) => { };
                    git.Process.BeginErrorReadLine();
                }

                string data = git.StandardOutput.ReadToEnd();
                git.WaitForExit();

                switch (git.ExitCode)
                {
                    case 0: // OK
                        return data.TrimEnd();
                    case 128: // Not inside a Git repository
                        return null;
                    default:
                        var message = "Failed to get current Git repository";
                        _trace.WriteLine($"{message} (exit={git.ExitCode})");
                        throw CreateGitException(git, message, _trace2);
                }
            }
        }

        public IEnumerable<GitRemote> GetRemotes()
        {
            using (var git = CreateProcess("remote -v show"))
            {
                // Redirect stderr so we can check for 'not a git repository' errors
                git.StartInfo.RedirectStandardError = true;
                git.Start(Trace2ProcessClass.Git);
                // To avoid deadlocks, always read the output stream first and then wait
                // TODO: don't read in all the data at once; stream it
                string data = git.StandardOutput.ReadToEnd();
                string stderr = git.StandardError.ReadToEnd();
                git.WaitForExit();

                switch (git.ExitCode)

View on GitHub (pinned to e8ce762cd0)