{"record":{"id":"8561433170ccaa01","repo":"mislav/hub","slug":"fatal-not-a-git-repository","errorCode":null,"errorMessage":"fatal: Not a git repository","messagePattern":"fatal: Not a git repository","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"github/localrepo.go","lineNumber":16,"sourceCode":"package github\n\nimport (\n\t\"fmt\"\n\t\"net/url\"\n\t\"strings\"\n\n\t\"github.com/github/hub/v2/git\"\n)\n\nfunc LocalRepo() (repo *GitHubRepo, err error) {\n\trepo = &GitHubRepo{}\n\n\t_, err = git.Dir()\n\tif err != nil {\n\t\terr = fmt.Errorf(\"fatal: Not a git repository\")\n\t\treturn\n\t}\n\n\treturn\n}\n\ntype GitHubRepo struct {\n\tremotes []Remote\n}\n\nfunc (r *GitHubRepo) loadRemotes() error {\n\tif r.remotes != nil {\n\t\treturn nil\n\t}\n\n\tremotes, err := Remotes()\n\tif err != nil {\n\t\treturn err","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/mislav/hub/blob/5c547ed804368763064e51f3990851e267e88edd/github/localrepo.go#L1-L34","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ncmd := exec.Command(\"hub\", \"browse\") // run in ~/\n// after\ncwd, _ := os.Getwd()\nif _, err := os.Stat(filepath.Join(cwd, \".git\")); err != nil {\n    // bail out or cd into the repo first\n}\ncmd := exec.Command(\"hub\", \"browse\")","handlingStrategy":"validation","validationCode":"if _, err := os.Stat(filepath.Join(cwd, \".git\")); err != nil {\n    // not a git repository; abort before calling github.LocalRepo()\n}","typeGuard":null,"tryCatchPattern":"repo, err := github.LocalRepo()\nif err != nil {\n    if strings.Contains(err.Error(), \"Not a git repository\") {\n        return fmt.Errorf(\"run this command inside a git repository\")\n    }\n    return err\n}","preventionTips":["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"],"tags":["git","repository-context","working-directory"],"backgroundTag":"not-a-git-repository","analyzedSha":"5c547ed804368763064e51f3990851e267e88edd","analyzedAt":"2026-09-01T03:34:15.525Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}