gitbutlerapp/gitbutler · error

cannot restore a snapshot without checkout identity outside…

Error message

cannot restore a snapshot without checkout identity outside the workspace branch

What it means

Thrown by `restore_snapshot` as the legacy guard for snapshots that carry neither a reconstructed checkout identity nor a workspace commit, while HEAD is not on the workspace branch. Outside the workspace branch GitButler cannot infer which ref/commit to check out when restoring, so it refuses rather than guessing.

Solutions

  1. Switch to the workspace branch before restoring (`but switch` / `git checkout <workspace-branch>`)
  2. Use a newer snapshot entry that includes checkout identity (created by a current GitButler version)
  3. Take a fresh snapshot while on the workspace branch, then restore from that entry
  4. Upgrade GitButler so snapshots are written with explicit checkout identity

Example fix

// before: restore on feature-branch with legacy snapshot -> bail
// after:
//   git checkout main   # the workspace branch
//   but oplog restore   # proceeds
Defensive patterns

Strategy: validation

Validate before calling

// only restore when HEAD is the workspace branch (legacy snapshots)
let head_ref = repo.head()?.referent_name().map(|n| n.to_owned());
if head_ref.as_deref() != Some(workspace_ref) {
    return Err("switch to the workspace branch before restoring this snapshot");
}

Prevention

When it happens

Trigger: `restore_snapshot` runs with `restored_checkout.is_none()` and `head_ref != workspace_ref` (HEAD on a non-workspace local branch or, per the preceding check, detached HEAD already bailed) while the snapshot lacks checkout identity.

Common situations: Restoring very old snapshots (pre checkout-identity format) while on a regular feature branch; running restore from a repo where the user manually switched branches away from the GitButler workspace; snapshots created by older GitButler versions.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at crates/gitbutler-oplog/src/oplog.rs:1160

        {
            bail!("snapshot checkout and workspace commits disagree");
        }
    }
    // Managed snapshots already identify their checkout through the workspace commit entry.
    let restored_checkout = restored_checkout.or_else(|| {
        restored_workspace_commit.map(|commit_id| SnapshotCheckout {
            ref_name: workspace_ref.to_owned(),
            commit_id,
        })
    });

    let head = repo.head()?;
    let head_ref = head
        .referent_name()
        .context("We will not change a worktree in detached HEAD state")?;
    // Snapshots with neither checkout identity nor a workspace commit retain the old guard.
    if restored_checkout.is_none() && head_ref != workspace_ref {
        bail!("cannot restore a snapshot without checkout identity outside the workspace branch");
    }

    let gix_repo = ctx.clone_repo_for_merging()?;
    let workdir_tree_id = get_workdir_tree(None, snapshot_commit_id, &gix_repo)?;

    // Check out the snapshot's worktree while HEAD still points at the pre-restore commit:
    // safe_checkout diffs from `before_restore_snapshot_workdir_tree_id`, so
    // the workspace ref is repointed only afterwards (below).
    but_core::worktree::safe_checkout_from_head(
        workdir_tree_id,
        &gix_repo,
        but_core::worktree::checkout::Options {
            // `workdir_tree_id` is the restored snapshot's full workdir tree, so it already
            // contains the uncommitted changes captured at that point. `safe_checkout_from_head`
            // otherwise re-applies the current uncommitted changes on top via a 3-way merge whose
            // base is `HEAD^{tree}`, which makes those changes collide with the identical ones
            // already in the destination. Using the pre-restore workdir tree as the merge base
            // means the current uncommitted changes equal the base and cancel out, so the restored

View on GitHub (pinned to 58e5313667)