nikivdev/code · error
{} is not inside a git repository
Error message
{} is not inside a git repository What it means
When resolving the repository root, the tool runs `git rev-parse --show-toplevel` from the starting directory. If git exits non-zero, the directory is not inside a git work tree, and this error names the offending path.
Source
Thrown at src/pr_preview.rs:1344
let head_sha = git_rev_parse(repo_root, head_ref)?;
Ok(ResolvedBase {
requested: requested.to_string(),
resolved: head_ref.to_string(),
merge_base: head_sha,
compare_label: compare_head_label.to_string(),
fallback_used: true,
})
}
fn resolve_git_repo_root(start: &Path) -> Result<PathBuf> {
let output = Command::new("git")
.current_dir(start)
.args(["rev-parse", "--show-toplevel"])
.output()
.with_context(|| format!("failed to run git rev-parse in {}", start.display()))?;
if !output.status.success() {
bail!("{} is not inside a git repository", start.display());
}
Ok(PathBuf::from(
String::from_utf8_lossy(&output.stdout).trim(),
))
}
fn resolve_preview_context(start: &Path) -> Result<PreviewContext> {
if let Ok(repo_root) = resolve_git_repo_root(start) {
let head_label = detect_head_label(&repo_root)?;
return Ok(PreviewContext {
repo_root: repo_root.clone(),
review_root: repo_root,
head_ref: "HEAD".to_string(),
head_label,
compare_head_label: "HEAD".to_string(),
work_tree_root: None,
});
}View on GitHub (pinned to a747e741ae)
Solutions
- Run the command from inside a git repository or pass --path pointing at one
- Verify the target has a valid .git directory (`git -C <dir> rev-parse --show-toplevel`)
- Re-clone the repository if .git is corrupted
Example fix
// before f pr preview --path /tmp/scratch # not a git repo // after cd ~/repos/api && f pr preview # inside a git repo
Defensive patterns
Strategy: validation
Validate before calling
let out = std::process::Command::new("git")
.current_dir(path)
.args(["rev-parse", "--show-toplevel"])
.output()?;
if !out.status.success() {
anyhow::bail!("{} is not a git repo; fix --path", path.display());
} Type guard
fn is_git_repo(dir: &Path) -> bool {
std::process::Command::new("git")
.current_dir(dir)
.args(["rev-parse", "--show-toplevel"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
} Try / catch
if let Err(e) = run_pr_preview(cmd) {
if e.to_string().ends_with("is not inside a git repository") {
eprintln!("Point --path at a valid git work tree");
}
} Prevention
- Verify the target directory is a git work tree before invoking
- Don't pass scratch/export directories via --path
- Check .git exists and is not corrupted
When it happens
Trigger: Running `f pr preview` (with --path or from the cwd) pointing at a directory that is not part of any git repository, or a corrupt .git dir causing rev-parse to fail.
Common situations: Wrong --path pointing at a scratch/exported directory; running from a bare checkout's parent; .git directory deleted or corrupted; running outside any repo.
Related errors
- derived shared repo root {} does not contain .git
- jj git export retry loop should always return
- Queued commit {} is not at HEAD (current HEAD is {}). Checko
- Queued commit was created on branch {} but current branch is
- env file not found: {}
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/844de9ad51c7fc84.
Report an issue: GitHub.