gitbutlerapp/gitbutler · error

Cannot unapply workspace reference

Error message

Cannot unapply workspace reference '{}' without switching away from it

What it means

`unapply_workspace_reference` refuses to remove the currently checked-out workspace reference unless the caller explicitly declared, via `WorkspaceDisposition`, that it is allowed to switch the worktree away to another commit. This prevents silently changing what HEAD points at.

Solutions

  1. Pass a `WorkspaceDisposition` that permits switching away from the workspace (e.g. the variant with `may_switch_away_from_workspace: true`)
  2. Unapply only non-workspace branches, keeping the workspace reference intact
  3. Switch to another branch first if using a strict disposition is intentional

Example fix

// before
unapply_workspace_reference(ws, repo, meta, ws_ref, WorkspaceDisposition::default())?;
// after
let disposition = WorkspaceDisposition { may_switch_away_from_workspace: true, .. };
unapply_workspace_reference(ws, repo, meta, ws_ref, disposition)?;
Defensive patterns

Strategy: validation

Validate before calling

let disposition = WorkspaceDisposition { may_switch_away_from_workspace: true, .. };
unapply_workspace_reference(ws, repo, meta, ws_ref, disposition)?;

Prevention

When it happens

Trigger: Calling unapply on the workspace branch itself while `disposition.may_switch_away_from_workspace()` returns false (e.g. default/strict disposition passed by the caller).

Common situations: Tooling that wants to dissolve an entire workspace but hasn't opted into switching the user's checkout; automated pipelines running unapply against the active workspace ref.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

    /// Unapply the managed workspace reference itself.
    ///
    /// - Run before normal branch removal because the workspace ref is a container, not a stack segment.
    /// - Reject dispositions that keep the workspace ref because unapplying it requires switching away.
    /// - Pick a named checkout target from the current projection.
    /// - Reject GitButler-conflicted target commits with a ref-aware error.
    /// - Safely checkout the target commit, then switch `HEAD` to the target ref.
    /// - Optionally delete the managed workspace ref and metadata.
    /// - Retraverse from the target ref so the returned workspace matches the new `HEAD`.
    fn unapply_workspace_reference(
        ws: &but_graph::Workspace,
        repo: &gix::Repository,
        meta: &mut impl RefMetadata,
        workspace_ref_name: &FullNameRef,
        disposition: WorkspaceDisposition,
    ) -> anyhow::Result<Outcome<'static>> {
        if !disposition.may_switch_away_from_workspace() {
            bail!(
                "Cannot unapply workspace reference '{}' without switching away from it",
                workspace_ref_name.shorten()
            );
        }

        let ref_to_checkout = ref_to_checkout_after_workspace_unapply(ws)?;
        safe_checkout_ref_to_checkout(
            repo,
            &ref_to_checkout,
            but_core::worktree::checkout::Options {
                skip_head_update: true,
                ..Default::default()
            },
        )?;

        let workspace_ref_expected = ws
            .tip_commit()
            .context("BUG: unborn should be impossible here")?

View on GitHub (pinned to 58e5313667)