gitbutlerapp/gitbutler · error
Couldn't find workspace head.
Error message
Couldn't find workspace head.
What it means
tear_off_branch() detaches a branch from its workspace stack, which requires rebasing against the workspace head commit. When the workspace graph has no tip commit (e.g. an empty workspace or a workspace without a managed workspace commit), the function cannot establish a base for the rebase and bails with this message.
Solutions
- Ensure the workspace has at least one commit (a workspace head) before tearing off branches
- Create a commit in the workspace so tip_commit() returns Some
- Skip tear-off for empty workspaces and use a different branch-management API
Example fix
// before
let outcome = tear_off_branch(editor, ref_name);
// after
if workspace.tip_commit().is_none() {
anyhow::bail!("workspace has no head commit; cannot tear off branch");
}
let outcome = tear_off_branch(editor, ref_name); Defensive patterns
Strategy: validation
Validate before calling
if workspace.tip_commit().is_none() {
// skip tear-off; workspace has no head commit
} Type guard
let has_head = workspace.tip_commit().map(|c| c.id).is_some();
Try / catch
match tear_off_branch(editor, ref_name) {
Err(e) if e.to_string().contains("Couldn't find workspace head") => handle_empty_workspace(),
other => other?,
} Prevention
- Check tip_commit() before any stack-reordering operation
- Never operate on workspaces with zero commits
- Recreate the workspace commit after operations that empty the stack
When it happens
Trigger: Calling but_workspace::branch::tear_off_branch() on a workspace whose tip_commit() returns None - typically an empty workspace with no commits, or a workspace reference that points to nothing.
Common situations: Operating on a freshly initialized or emptied workspace stack where all commits were removed; tearing off a branch before any workspace commit exists; workspace ref resolving to an unborn/invalid commit.
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
- Expected to be in open workspace mode
- Refusing to apply a reference that already is a workspace
- Ad-hoc (single-branch) branch moves are not supported…
- Branch ' ' cannot be created: the target commit ( ) already…
- BranchNotFound
AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18).
Data as JSON: /api/errors/b7f4a31d2fe60678.
Report an issue: GitHub.
Appendix: source
Thrown at crates/but-workspace/src/branch/move_branch.rs:100
}
};
let mut ws_meta = workspace.metadata.clone();
let (source_stack, subject_segment) = source;
if source_stack.segments.len() == 1 {
// There's only one branch in the source stack. Nothing to do.
return Ok(Outcome {
rebase: editor.rebase()?,
ws_meta,
new_tip: None,
branch_stack_order: None,
});
}
let Some(workspace_head) = workspace.tip_commit().map(|commit| commit.id) else {
bail!("Couldn't find workspace head.")
};
let head_selector = editor
.select_commit(workspace_head)
.context("Failed to find the workspace head in the graph.")?;
let Some(lower_bound_ref) = workspace
.lower_bound_segment_id
.map(|segment_id| &workspace.graph[segment_id])
.and_then(|segment| segment.ref_name())
else {
bail!("Tearing off a branch requires a workspace common base");
};
let target_selector = editor
.select_reference(lower_bound_ref)
.context("Failed to find target reference in graph.")?;
let DisconnectParameters {View on GitHub (pinned to 58e5313667)