gitbutlerapp/gitbutler · error

failed to discover GitButler branch with 'but --json status

Error message

failed to discover GitButler branch with 'but --json status': {stderr}

What it means

This error is thrown when the agentlog skim tool runs `but --json status` as a subprocess to discover the GitButler branch, and the CLI exits with a non-zero status. The stderr from the failed CLI invocation is embedded in the message to explain why target discovery failed. It is a wrapper around any failure of the underlying `but status` command, so the real cause is in the captured stderr.

Solutions

  1. Read the `{stderr}` portion of the message to find the actual `but --json status` failure cause
  2. Run `but --json status` manually in the target directory to reproduce and diagnose
  3. Ensure the working directory is a GitButler-initialized workspace (`but init` if needed)
  4. Verify the `but` binary on PATH is the expected version

Example fix

// before
let target = applied_gitbutler_branch(dir)?;
// after
let target = match applied_gitbutler_branch(dir) {
    Ok(t) => t,
    Err(e) if e.to_string().contains("failed to discover GitButler branch") => {
        eprintln!("skipping agentlog skim: not in a GitButler workspace: {e}");
        return Ok(None);
    }
    Err(e) => return Err(e),
};
Defensive patterns

Strategy: try-catch

Validate before calling

const out = await run("but", ["--json", "status"], { stdio: "ignore" });
if (out.status !== 0) throw new Error("but CLI unavailable or not a GitButler workspace");

Type guard

function isGitButlerWorkspace(dir: string): boolean {
  return existsSync(join(dir, ".git", "gitbutler"));
}

Try / catch

try {
  const target = await resolveDefaultBranchTarget(dir);
} catch (e) {
  if (String(e).includes("failed to discover GitButler branch")) {
    logger.warn({ stderr: String(e) }, "agentlog skim skipped");
    return null;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `applied_gitbutler_branch` (via `resolve_default_branch_target`) in a directory where `but --json status` fails: not inside a Git workspace, workspace not initialized with GitButler, corrupted metadata, or any CLI-side error that makes `but status` exit non-zero.

Common situations: Running the agentlog skim outside a git repository; running before `but init`; PATH resolving to an incompatible or broken `but` binary; lock contention or workspace corruption surfacing as stderr output.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


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

Appendix: source

Thrown at crates/but-agentlog/src/skim.rs:355

}

#[derive(Deserialize)]
struct StatusBranch {
    name: String,
}

fn applied_gitbutler_branch(workdir: &Path) -> anyhow::Result<String> {
    let but_path = std::env::current_exe().context("failed to locate current executable")?;
    let output = ProcessCommand::new(&but_path)
        .arg("-C")
        .arg(workdir)
        .args(["--json", "status"])
        .stdin(Stdio::null())
        .output()
        .context("failed to run 'but --json status' for agentlog skim target discovery")?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("failed to discover GitButler branch with 'but --json status': {stderr}");
    }
    let status: StatusReport =
        serde_json::from_slice(&output.stdout).context("failed to parse 'but --json status'")?;
    status
        .stacks
        .into_iter()
        .flat_map(|stack| stack.branches)
        .map(|branch| branch.name)
        .next()
        .context("no applied GitButler branch found; pass an explicit skim target")
}

View on GitHub (pinned to 58e5313667)