gitbutlerapp/gitbutler · error

Cannot unapply a branch from an ad-hoc detached workspace

Error message

Cannot unapply a branch from an ad-hoc detached workspace

What it means

Unapply requires a named workspace reference to update workspace metadata and rewrite references. A workspace that is ad-hoc purely because it has no name (detached, unnamed workspace) cannot support the bookkeeping unapply performs, so the operation is rejected.

Solutions

  1. Create/assign a named workspace reference first (apply operation) and retry unapply there
  2. Check out a branch instead of a detached commit before using workspace operations
  3. Skip unapply for unnamed workspaces in tooling that enumerates workspaces

Example fix

// before
unapply(repo, meta, &ws, branch, ...)?; // fails when ws.ref_name() is None
// after
if ws.ref_name().is_none() {
    anyhow::bail!("operation requires a named workspace");
}
unapply(repo, meta, &ws, branch, ...)?;
Defensive patterns

Strategy: validation

Validate before calling

if ws.ref_name().is_none() {
    anyhow::bail!("unapply requires a named workspace");
}

Prevention

When it happens

Trigger: Calling `unapply` when `ws.ref_name()` is `None` — i.e., operating on a detached-HEAD-style ad-hoc workspace with no workspace reference.

Common situations: Operating on a repository where the user checked out a commit directly (detached HEAD) and the workspace was built ad-hoc; automation running before any workspace reference was created.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at crates/but-workspace/src/branch/unapply.rs:248

                workspace_ref_name.as_ref(),
                workspace_disposition,
            );
        }

        let branch_in_ws = ws.find_segment_and_stack_by_refname(branch);
        if branch_in_ws.is_none() {
            if branch_ref.is_none() {
                bail!(
                    "Cannot unapply non-existing branch '{branch}'",
                    branch = branch.shorten()
                );
            }
            // The branch exists in Git, but does not in the workspace: Nothing to do.
            return Ok(Outcome::new(Cow::Borrowed(workspace)));
        }
        let Some(workspace_ref_name) = workspace_ref_name else {
            // This is an ad-hoc workspace by merit of being unnamed.
            bail!("Cannot unapply a branch from an ad-hoc detached workspace");
        };
        let mut ws_md = meta.workspace(workspace_ref_name.as_ref())?;
        if ws.kind.has_managed_ref() || ws.has_metadata() {
            ws.reconcile_metadata(&mut ws_md)?;
        }
        let branch_removed_from_ws_meta = ws_md.unapply_branch(branch);
        if !branch_removed_from_ws_meta {
            // The branch wasn't in workspace metadata, yet it was present, so also delete its branch metadata
            // as it could be used to disambiguate the segment.
            // TODO: this will actually be observable even if it doens't work, unless it's run in a transaction, which right now it's not!
            //       Should be able to redo the traversal with an overlay that hides branch metadata, but I'd say it's not important enough.
            meta.remove(branch)?;
            let graph = ws
                .graph
                .redo_traversal_with_overlay(repo, meta, Overlay::default())?;
            let workspace = graph.into_workspace()?;
            if workspace.refname_is_segment(branch) {
                bail!(

View on GitHub (pinned to 58e5313667)