gitbutlerapp/gitbutler · error

Worktree has no usable HEAD

Error message

Worktree {name} has no usable HEAD

What it means

After the archived check, `active_worktree` verifies the worktree has a usable HEAD via `ctx.worktree_head(...)`. If there is none — the worktree is an unborn branch, checked out on a workspace ref, or its HEAD is broken — there is nothing to open or diff, so the operation is refused.

Solutions

  1. Create an initial commit or check out an existing branch in the worktree so it has a usable HEAD
  2. Detach the worktree from the workspace ref onto a regular branch before using it as a change source
  3. Repair or recreate the worktree if its HEAD file is broken (git worktree repair / remove and re-add)

Example fix

// before
await api.openChangesSource(ctx, 'empty-wt'); // unborn HEAD
// after
await runGit(['-C', 'wt-path', 'checkout', '-b', 'main', 'origin/main']); // give it a HEAD
await api.openChangesSource(ctx, 'empty-wt');
Defensive patterns

Strategy: validation

Validate before calling

if ((await api.worktreeHead(ctx, name)) == null) {
  throw new Error('Worktree has no usable HEAD; check out a branch or commit first');
}

Type guard

null

Try / catch

try {
  await api.openChangesSource(ctx, name);
} catch (e) {
  if (e.message.includes('no usable HEAD')) await checkoutBranchInWorktree(name);
}

Prevention

When it happens

Trigger: Calling `open_changes_source` for a worktree whose HEAD cannot be resolved: a freshly created worktree on an unborn branch, a worktree whose HEAD points at a workspace ref, or a broken .git/HEAD file.

Common situations: A worktree was just created without an initial commit; the worktree was attached to a GitButler workspace rather than a normal branch; repository corruption or manual .git editing left HEAD dangling.

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/451250bb3b2610a7. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-api/src/worktrees.rs:98

/// Look up the *active* linked worktree named `name`.
///
/// Every command here operates on active worktrees only - an archived one is
/// hidden from the graph, so operations against it could not be materialized.
///
/// Must not be called while a database handle is borrowed, see
/// [`but_ctx::Context::worktrees_with_state()`].
fn active_worktree(ctx: &but_ctx::Context, name: &str) -> Result<WorktreeEntry> {
    let worktree = ctx
        .worktrees_with_state()?
        .into_iter()
        .find(|worktree| worktree.name == name.as_bytes())
        .with_context(|| format!("Worktree {name} does not exist"))?;
    if worktree.archived {
        bail!("Worktree {name} is archived");
    }
    if ctx.worktree_head(worktree.name.as_bstr())?.is_none() {
        // Unborn, workspace-ref checkout, or broken - nothing to operate on.
        bail!("Worktree {name} has no usable HEAD");
    }
    Ok(worktree)
}

/// Open the checkout that `source` reads its changes from, returning its stable
/// name along with a plain from-disk open of it, or `None` for the main worktree.
///
/// Callers turn this into a [`ChangeSource`](but_workspace::commit::ChangeSource)
/// for the duration of an editor-backed operation.
///
/// Must not be called while a database handle is borrowed, see
/// [`but_ctx::Context::worktrees_with_state()`].
pub(crate) fn open_changes_source(
    ctx: &but_ctx::Context,
    source: &ChangesSource,
) -> Result<Option<(BString, gix::Repository)>> {
    let ChangesSource::Worktree(name) = source else {
        return Ok(None);

View on GitHub (pinned to 58e5313667)