gitbutlerapp/gitbutler · error

No git repository found.

Error message

No git repository found.

What it means

This error is raised by `setup_new_repo` in the `but` CLI after it failed to find an existing git repository and failed to initialize a new one (e.g. because the directory was not eligible for init or no path was usable). It is a terminal fallback: every code path that could produce a usable repository returns earlier, so reaching this line means no repository could be located or created.

Solutions

  1. cd into (or point the command at) a directory that is inside a git repository, or run `git init` first
  2. verify with `git rev-parse --show-toplevel` that the current directory belongs to a repo
  3. check that the intended workspace directory exists and is writable before invoking the command

Example fix

// before (shell)
but status   # run in ~/projects/plain-folder
// after
cd ~/projects/my-repo && but status
Defensive patterns

Strategy: validation

Validate before calling

if !std::path::Path::new(dir).join(".git").exists() {
    // walk up parents or run `git rev-parse --show-toplevel` first
    eprintln!("Not inside a git repository: {dir}");
}

Try / catch

match find_or_initialize_repo(ctx) {
    Err(e) if e.to_string().contains("No git repository found") => {
        eprintln!("Run from inside a repo, or `git init` first.");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Running `but` commands in a directory that is not inside any git repository (no `.git` anywhere up the tree) when the auto-init path also fails, or calling `find_or_initialize_repo` with a working directory that does not permit repository creation.

Common situations: Running `but status` or similar in a plain folder, a typo'd `--dir`/cwd outside the repo, running in a container where the project was not volume-mounted, or running before `git init` when initialization was skipped.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/2e186385e0df6ecd. Report an issue: GitHub.

Appendix: source

Thrown at crates/but/src/command/legacy/setup.rs:693

                "{}",
                t.hint
                    .paint("Initializing new repository and creating an empty first commit...")
            )?;
            let repo = gix::init(current_dir)?;

            create_empty_initial_commit(&repo)?;

            writeln!(
                &mut progress as &mut dyn FmtWrite,
                "{}",
                t.success
                    .paint("Initialized a new repository and created an empty first commit.\n")
            )?;
            return Ok(repo);
        }
    }

    Err(anyhow::anyhow!("No git repository found."))
}

fn create_empty_initial_commit(repo: &gix::Repository) -> anyhow::Result<()> {
    // In an unborn repo, this returns the well-known empty-tree id.
    // (It works even if the empty tree object isn’t physically in the ODB.)
    let empty_tree = repo.head_tree_id_or_empty().expect("repo access failed"); // -> Id<'_>
    let empty_tree = empty_tree.detach(); // -> ObjectId (optional; commit() accepts Into<ObjectId> anyway)

    // No parents for the first commit. Update HEAD (writes through to refs/heads/main).
    repo.commit(
        "HEAD",
        "Initial empty commit\n",
        empty_tree,
        std::iter::empty::<gix::hash::ObjectId>(),
    )?;

    Ok(())
}

View on GitHub (pinned to 58e5313667)