nikivdev/code · error
not inside a git repository
Error message
not inside a git repository
What it means
Thrown by `repo_root()` (used by `top_file_path`) when `git rev-parse --show-toplevel` exits non-zero, which happens when the current working directory is not inside a git repository or work tree. The library needs the repo root to locate its top-entries file, so it cannot proceed.
Source
Thrown at src/commits.rs:544
Ok(())
}
fn top_hashes(entries: &[TopEntry]) -> HashSet<String> {
entries.iter().map(|entry| entry.hash.clone()).collect()
}
fn top_file_path() -> Result<PathBuf> {
let root = repo_root()?;
Ok(root.join(TOP_COMMITS_PATH))
}
fn repo_root() -> Result<PathBuf> {
let output = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.output()
.context("failed to run git rev-parse")?;
if !output.status.success() {
bail!("not inside a git repository");
}
Ok(Path::new(String::from_utf8_lossy(&output.stdout).trim()).to_path_buf())
}
View on GitHub (pinned to a747e741ae)
Solutions
- `cd` into a directory inside a git repository (one containing a valid .git)
- Run `git rev-parse --show-toplevel` yourself to confirm git sees the repo
- If .git is broken, restore it (`git init` if appropriate, or re-clone)
- Check that GIT_DIR/GIT_WORK_TREE env vars are not misdirecting git
Example fix
// before (shell) ~/scratch $ tool commits top // not inside a git repository // after (shell) ~/scratch $ cd ~/projects/myrepo ~/projects/myrepo $ tool commits top
Defensive patterns
Strategy: validation
Validate before calling
// check before invoking any commits subcommand
let out = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.output()?;
if !out.status.success() {
eprintln!("current directory is not a git repository; cd into a repo first");
std::process::exit(1);
} Type guard
fn inside_git_repo() -> bool {
Command::new("git")
.args(["rev-parse", "--is-inside-work-tree"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
} Try / catch
match run_top_command() {
Err(e) if e.to_string() == "not inside a git repository" => {
eprintln!("Run this from inside a git repository (cwd: {:?})", std::env::current_dir()?);
std::process::exit(1);
}
other => other?,
} Prevention
- Check `git rev-parse --is-inside-work-tree` before running repo-scoped tools
- Avoid running the tool in $HOME or scratch directories
- Verify GIT_DIR/GIT_WORK_TREE env overrides are intentional
- Re-clone or `git init` if .git is missing/corrupt
When it happens
Trigger: Invoking any commits/top subcommand from a directory outside a git work tree, inside a bare repository's internals, or in a directory whose .git is corrupt/missing so rev-parse fails.
Common situations: Running the tool in $HOME or a temp dir, running in a submodule-less bare clone, .git directory deleted or corrupted, or GIT_DIR pointing somewhere invalid in the environment.
Related errors
- failed to resolve git repo root
- git repo root was empty
- git status failed with {}
- Not a git repository
- Not a git repository
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/8b873867a30b39ba.
Report an issue: GitHub.