mislav/hub · error

Not a git repository (or any of the parent directories): .gi

Error message

Not a git repository (or any of the parent directories): .git

What it means

Dir() runs `git rev-parse -q --git-dir` to locate the .git directory; when the command fails, the library concludes the current directory is not inside a git repository and throws this fixed message. It deliberately discards the underlying error to present a single, predictable cause.

Source

Thrown at git/git.go:34

	output, err := versionCmd.Output()
	if err != nil {
		return "", fmt.Errorf("error running git version: %s", err)
	}
	return firstLine(output), nil
}

var cachedDir string

func Dir() (string, error) {
	if cachedDir != "" {
		return cachedDir, nil
	}

	dirCmd := gitCmd("rev-parse", "-q", "--git-dir")
	dirCmd.Stderr = nil
	output, err := dirCmd.Output()
	if err != nil {
		return "", fmt.Errorf("Not a git repository (or any of the parent directories): .git")
	}

	var chdir string
	for i, flag := range GlobalFlags {
		if flag == "-C" {
			dir := GlobalFlags[i+1]
			if filepath.IsAbs(dir) {
				chdir = dir
			} else {
				chdir = filepath.Join(chdir, dir)
			}
		}
	}

	gitDir := firstLine(output)

	if !filepath.IsAbs(gitDir) {
		if chdir != "" {

View on GitHub (pinned to 5c547ed804)

Solutions

  1. Run `git init` in the directory (or cd into an actual repository)
  2. Verify with `git rev-parse --git-dir` in the same directory
  3. Pass a valid path with the -C global flag if relying on GlobalFlags
  4. Check that the repository wasn't cloned with an incomplete/failed checkout

Example fix

// before
dir, err := git.Dir()
// after: guard beforehand
if _, err := os.Stat(".git"); err != nil {
    if err := run("git", "init"); err != nil {
        return err
    }
}
dir, err := git.Dir()
Defensive patterns

Strategy: validation

Validate before calling

func inGitRepo() bool {
    dir, err := filepath.Abs(".")
    if err != nil { return false }
    for {
        if fi, err := os.Stat(filepath.Join(dir, ".git")); err == nil && (fi.IsDir() || fi.Mode().IsRegular()) {
            return true
        }
        parent := filepath.Dir(dir)
        if parent == dir { return false }
        dir = parent
    }
}

Try / catch

dir, err := git.Dir()
if err != nil {
    return fmt.Errorf("this command must run inside a git repository (run `git init` if needed)")
}

Prevention

When it happens

Trigger: Calling git.Dir() (directly or via create, HasFile, TestGitDir, NewEditor, LocalRepo) while the working directory — or any directory given via -C global flags — is outside any git work tree (no .git found walking up parents).

Common situations: Running the tool in a brand-new empty project before `git init`; running from $HOME or /tmp; typo in the -C path; running in a submodule-less directory; CI checking out with no history or with detached empty workspaces.

Related errors


AI-assisted analysis of mislav/hub@5c547ed804 (2026-09-01). Data as JSON: /api/errors/c81d911ecb36ee7a. Report an issue: GitHub.