{"record":{"id":"25f605181a1a28e3","repo":"gitbutlerapp/gitbutler","slug":"cannot-restore-a-snapshot-without-checkout-identit","errorCode":null,"errorMessage":"cannot restore a snapshot without checkout identity outside the workspace branch","messagePattern":"cannot restore a snapshot without checkout identity outside the workspace branch","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gitbutler-oplog/src/oplog.rs","lineNumber":1160,"sourceCode":"        {\n            bail!(\"snapshot checkout and workspace commits disagree\");\n        }\n    }\n    // Managed snapshots already identify their checkout through the workspace commit entry.\n    let restored_checkout = restored_checkout.or_else(|| {\n        restored_workspace_commit.map(|commit_id| SnapshotCheckout {\n            ref_name: workspace_ref.to_owned(),\n            commit_id,\n        })\n    });\n\n    let head = repo.head()?;\n    let head_ref = head\n        .referent_name()\n        .context(\"We will not change a worktree in detached HEAD state\")?;\n    // Snapshots with neither checkout identity nor a workspace commit retain the old guard.\n    if restored_checkout.is_none() && head_ref != workspace_ref {\n        bail!(\"cannot restore a snapshot without checkout identity outside the workspace branch\");\n    }\n\n    let gix_repo = ctx.clone_repo_for_merging()?;\n    let workdir_tree_id = get_workdir_tree(None, snapshot_commit_id, &gix_repo)?;\n\n    // Check out the snapshot's worktree while HEAD still points at the pre-restore commit:\n    // safe_checkout diffs from `before_restore_snapshot_workdir_tree_id`, so\n    // the workspace ref is repointed only afterwards (below).\n    but_core::worktree::safe_checkout_from_head(\n        workdir_tree_id,\n        &gix_repo,\n        but_core::worktree::checkout::Options {\n            // `workdir_tree_id` is the restored snapshot's full workdir tree, so it already\n            // contains the uncommitted changes captured at that point. `safe_checkout_from_head`\n            // otherwise re-applies the current uncommitted changes on top via a 3-way merge whose\n            // base is `HEAD^{tree}`, which makes those changes collide with the identical ones\n            // already in the destination. Using the pre-restore workdir tree as the merge base\n            // means the current uncommitted changes equal the base and cancel out, so the restored","sourceCodeStart":1142,"sourceCodeEnd":1178,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-oplog/src/oplog.rs#L1142-L1178","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Switch to the workspace branch before restoring (`but switch` / `git checkout <workspace-branch>`)","Use a newer snapshot entry that includes checkout identity (created by a current GitButler version)","Take a fresh snapshot while on the workspace branch, then restore from that entry","Upgrade GitButler so snapshots are written with explicit checkout identity"],"exampleFix":"// before: restore on feature-branch with legacy snapshot -> bail\n// after:\n//   git checkout main   # the workspace branch\n//   but oplog restore   # proceeds","handlingStrategy":"validation","validationCode":"// only restore when HEAD is the workspace branch (legacy snapshots)\nlet head_ref = repo.head()?.referent_name().map(|n| n.to_owned());\nif head_ref.as_deref() != Some(workspace_ref) {\n    return Err(\"switch to the workspace branch before restoring this snapshot\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check `git branch --show-current` equals the workspace branch before restoring","Avoid detached HEAD states during restore operations","Upgrade GitButler so snapshots carry explicit checkout identity","Prefer restoring recent snapshots created on the workspace branch"],"tags":["git","snapshot","head-state","restore"],"backgroundTag":"invalid-state-transition","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"}