{"record":{"id":"451250bb3b2610a7","repo":"gitbutlerapp/gitbutler","slug":"worktree-name-has-no-usable-head","errorCode":null,"errorMessage":"Worktree {name} has no usable HEAD","messagePattern":"Worktree (.+?) has no usable HEAD","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-api/src/worktrees.rs","lineNumber":98,"sourceCode":"/// Look up the *active* linked worktree named `name`.\n///\n/// Every command here operates on active worktrees only - an archived one is\n/// hidden from the graph, so operations against it could not be materialized.\n///\n/// Must not be called while a database handle is borrowed, see\n/// [`but_ctx::Context::worktrees_with_state()`].\nfn active_worktree(ctx: &but_ctx::Context, name: &str) -> Result<WorktreeEntry> {\n    let worktree = ctx\n        .worktrees_with_state()?\n        .into_iter()\n        .find(|worktree| worktree.name == name.as_bytes())\n        .with_context(|| format!(\"Worktree {name} does not exist\"))?;\n    if worktree.archived {\n        bail!(\"Worktree {name} is archived\");\n    }\n    if ctx.worktree_head(worktree.name.as_bstr())?.is_none() {\n        // Unborn, workspace-ref checkout, or broken - nothing to operate on.\n        bail!(\"Worktree {name} has no usable HEAD\");\n    }\n    Ok(worktree)\n}\n\n/// Open the checkout that `source` reads its changes from, returning its stable\n/// name along with a plain from-disk open of it, or `None` for the main worktree.\n///\n/// Callers turn this into a [`ChangeSource`](but_workspace::commit::ChangeSource)\n/// for the duration of an editor-backed operation.\n///\n/// Must not be called while a database handle is borrowed, see\n/// [`but_ctx::Context::worktrees_with_state()`].\npub(crate) fn open_changes_source(\n    ctx: &but_ctx::Context,\n    source: &ChangesSource,\n) -> Result<Option<(BString, gix::Repository)>> {\n    let ChangesSource::Worktree(name) = source else {\n        return Ok(None);","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-api/src/worktrees.rs#L80-L116","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Create an initial commit or check out an existing branch in the worktree so it has a usable HEAD","Detach the worktree from the workspace ref onto a regular branch before using it as a change source","Repair or recreate the worktree if its HEAD file is broken (git worktree repair / remove and re-add)"],"exampleFix":"// before\nawait api.openChangesSource(ctx, 'empty-wt'); // unborn HEAD\n// after\nawait runGit(['-C', 'wt-path', 'checkout', '-b', 'main', 'origin/main']); // give it a HEAD\nawait api.openChangesSource(ctx, 'empty-wt');","handlingStrategy":"validation","validationCode":"if ((await api.worktreeHead(ctx, name)) == null) {\n  throw new Error('Worktree has no usable HEAD; check out a branch or commit first');\n}","typeGuard":"null","tryCatchPattern":"try {\n  await api.openChangesSource(ctx, name);\n} catch (e) {\n  if (e.message.includes('no usable HEAD')) await checkoutBranchInWorktree(name);\n}","preventionTips":["Verify a worktree has a resolvable HEAD before using it as a change source","Avoid attaching worktrees to workspace refs for change-reading operations","Recreate broken worktrees instead of repairing .git/HEAD manually"],"tags":["worktree","head","unborn-branch"],"backgroundTag":"resource-not-found","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}