mislav/hub · error

fatal: Not a git repository

Error message

fatal: Not a git repository

What it means

LocalRepo() wraps a failure from git.Dir() (which runs `git rev-parse --git-dir`) into this message. It means the process's working directory is not inside a git repository, so no GitHubRepo can be constructed. The library aborts early because every subsequent operation (remotes, branches, projects) depends on git metadata.

Source

Thrown at github/localrepo.go:16

package github

import (
	"fmt"
	"net/url"
	"strings"

	"github.com/github/hub/v2/git"
)

func LocalRepo() (repo *GitHubRepo, err error) {
	repo = &GitHubRepo{}

	_, err = git.Dir()
	if err != nil {
		err = fmt.Errorf("fatal: Not a git repository")
		return
	}

	return
}

type GitHubRepo struct {
	remotes []Remote
}

func (r *GitHubRepo) loadRemotes() error {
	if r.remotes != nil {
		return nil
	}

	remotes, err := Remotes()
	if err != nil {
		return err

View on GitHub (pinned to 5c547ed804)

Solutions

  1. cd into a directory that is inside a git repository before calling the API
  2. Run `git rev-parse --git-dir` yourself to verify the directory is a repo
  3. If the repo is bare, set GIT_DIR appropriately or clone a working copy
  4. If the .git folder is missing, re-clone or `git init` as appropriate

Example fix

// before
cmd := exec.Command("hub", "browse") // run in ~/
// after
cwd, _ := os.Getwd()
if _, err := os.Stat(filepath.Join(cwd, ".git")); err != nil {
    // bail out or cd into the repo first
}
cmd := exec.Command("hub", "browse")
Defensive patterns

Strategy: validation

Validate before calling

if _, err := os.Stat(filepath.Join(cwd, ".git")); err != nil {
    // not a git repository; abort before calling github.LocalRepo()
}

Try / catch

repo, err := github.LocalRepo()
if err != nil {
    if strings.Contains(err.Error(), "Not a git repository") {
        return fmt.Errorf("run this command inside a git repository")
    }
    return err
}

Prevention

When it happens

Trigger: Calling LocalRepo() (directly or via apiCommand, browse, transformCheckoutArgs, transformCherryPickArgs, ciStatus, compare) when the current working directory is outside any git work tree, e.g. home directory, /tmp, or a plain folder.

Common situations: Running a hub command from a non-repo directory; a .git directory was deleted or the repo was moved; GIT_DIR points to an invalid path; running inside a bare-repo parent directory rather than the repo; scripts that cd before calling the library.

Related errors


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