nikivdev/code · error

not inside a git repository

Error message

not inside a git repository

What it means

Raised by git_root in src/push.rs when `git rev-parse --show-toplevel` exits non-zero, meaning the current working directory is not inside a git working tree. The mirror-push flow needs the repo root to run all subsequent git commands, so it aborts early with this message.

Source

Thrown at src/push.rs:326

pub(crate) fn normalize_git_url(url: &str) -> String {
    let url = url.trim();
    let url = if url.starts_with("git@github.com:") {
        url.replace("git@github.com:", "github.com/")
    } else if url.starts_with("https://github.com/") {
        url.replace("https://github.com/", "github.com/")
    } else {
        url.to_string()
    };
    url.trim_end_matches(".git").to_lowercase()
}

fn git_root() -> Result<PathBuf> {
    let output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .context("failed to locate git root")?;
    if !output.status.success() {
        bail!("not inside a git repository");
    }
    let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(PathBuf::from(path))
}

fn current_branch(repo_root: &Path) -> Result<String> {
    let output = Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .current_dir(repo_root)
        .output()
        .context("failed to read current branch")?;
    if !output.status.success() {
        bail!("git rev-parse --abbrev-ref HEAD failed");
    }
    let name = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if name.is_empty() || name == "HEAD" {
        bail!("detached HEAD (checkout a branch first)");
    }

View on GitHub (pinned to a747e741ae)

Solutions

  1. cd into your git repository root before running the command
  2. Run `git rev-parse --show-toplevel` yourself to confirm git sees a repo at your location
  3. If .git is missing, re-clone or run `git init` as appropriate

Example fix

// before
$ cd /tmp && f push
not inside a git repository
// after
$ cd ~/my-project && f push
Defensive patterns

Strategy: validation

Validate before calling

let inside = std::process::Command::new("git")
    .args(["rev-parse", "--show-toplevel"])
    .status()
    .map(|s| s.success())
    .unwrap_or(false);
if !inside { eprintln!("cd into your git repository before pushing"); }

Try / catch

match result {
    Err(e) if e.to_string().contains("not inside a git repository") => {
        eprintln!("run from a repo root");
    }
    Err(e) => return Err(e),
    Ok(v) => Ok(v),
}

Prevention

When it happens

Trigger: Invoking the push command from a directory outside any git repository, or inside a directory where git cannot determine a toplevel (e.g. .git removed, or a bare/uninitialized directory).

Common situations: Running `f push` from $HOME or a random folder instead of the project; the .git directory was deleted; running inside a submodule-style or bare checkout where --show-toplevel fails; typo'd working directory in a script.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/a2d9836602fba129. Report an issue: GitHub.