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 errView on GitHub (pinned to 5c547ed804)
Solutions
- cd into a directory that is inside a git repository before calling the API
- Run `git rev-parse --git-dir` yourself to verify the directory is a repo
- If the repo is bare, set GIT_DIR appropriately or clone a working copy
- 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
- Always run repository-dependent commands from inside a work tree
- Check `git rev-parse --git-dir` success before invoking the library
- In scripts, guard with `git rev-parse --is-inside-work-tree || exit 1`
- Beware commands that cd to another directory before calling the API
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
- Not a git repository (or any of the parent directories): .gi
- unable to determine git working directory
- Aborted: no revision could be determined from '%s'
- Error: %s/%s doesn't have a wiki
- the current branch '%s' doesn't seem pushed to a remote
AI-assisted analysis of mislav/hub@5c547ed804 (2026-09-01).
Data as JSON: /api/errors/8561433170ccaa01.
Report an issue: GitHub.