{"record":{"id":"37ffcdd4d960326f","repo":"gitbutlerapp/gitbutler","slug":"the-working-tree-differs-from-the-original-commit","errorCode":null,"errorMessage":"The working tree differs from the original commit. A forced abort is necessary.\\nIf you are seeing this message, please report it as a bug. The UI should have prevented this line getting hit.","messagePattern":"The working tree differs from the original commit\\. A forced abort is necessary\\.\\\\nIf you are seeing this message, please report it as a bug\\. The UI should have prevented this line getting hit\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gitbutler-edit-mode/src/lib.rs","lineNumber":281,"sourceCode":"    for ref_name in [EDIT_BRANCH_REF, UNCOMMITTED_CHANGES_REF] {\n        // Tolerate an already-absent ref, but surface real lookup failures.\n        if let Some(reference) = repo.try_find_reference(ref_name)? {\n            reference\n                .delete()\n                .with_context(|| format!(\"Failed to delete reference {ref_name}\"))?;\n        }\n    }\n    delete_edit_mode_metadata(ctx)?;\n    Ok(())\n}\n\npub(crate) fn abort_and_return_to_workspace(\n    ctx: &Context,\n    force: bool,\n    perm: &mut RepoExclusive,\n) -> Result<()> {\n    if !force && !changes_from_initial(ctx, perm.read_permission())?.is_empty() {\n        bail!(\n            \"The working tree differs from the original commit. A forced abort is necessary.\\nIf you are seeing this message, please report it as a bug. The UI should have prevented this line getting hit.\"\n        );\n    }\n\n    #[expect(deprecated, reason = \"checkout/materialization boundary\")]\n    let repo = &*ctx.git2_repo.get()?;\n\n    // Checkout gitbutler workspace branch\n    repo.set_head(WORKSPACE_BRANCH_REF)\n        .context(\"Failed to set head reference\")?;\n\n    let uncommited_changes = get_uncommitted_changes(&*ctx.repo.get()?)?;\n    let uncommited_changes = repo.find_tree(uncommited_changes.to_git2())?;\n\n    repo.checkout_tree(\n        uncommited_changes.as_object(),\n        Some(CheckoutBuilder::new().force().remove_untracked(true)),\n    )?;","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-edit-mode/src/lib.rs#L263-L299","documentation":"abort_and_return_to_workspace in gitbutler-edit-mode discards an edit-mode session and restores the pre-edit state. Before doing so it verifies the working tree still matches the initial commit of the edit session (via changes_from_initial). If the tree has diverged and `force` is false, aborting would silently destroy user work, so it bails and asks for a forced abort. The message notes the UI should normally prevent reaching this state.","triggerScenarios":"Calling abort_and_return_to_workspace(ctx, force=false, ...) while in edit mode after the working tree was modified outside the edit-mode tracking (e.g. files changed by an external editor/git command, or the edit session's saved changes were partially applied), so changes_from_initial returns non-empty.","commonSituations":"User edited files with an external tool while the app was in edit mode; a crash left edit-mode metadata pointing at a commit while the tree moved on; calling the internal API directly (CLI/tests) without first saving or checking out changes; stale UI state that believed the tree was clean.","solutions":["Call abort_and_return_to_workspace with force=true if you truly want to discard the divergence (the changes will be lost).","Before aborting, save/commit or stash the working-tree changes through the normal edit-mode exit path so nothing is lost.","Inspect the divergence with the starting_index_state/changes_from_initial API to confirm what differs before forcing.","Fix the caller (UI) to disable/force-guard the abort action when the tree is dirty."],"exampleFix":"// before\nabort_and_return_to_workspace(&ctx, false, &mut perm)?;\n// after\nlet dirty = changes_from_initial(&ctx, &perm).map(|c| !c.is_empty()).unwrap_or(false);\nabort_and_return_to_workspace(&ctx, dirty, &mut perm)?; // force only when needed","handlingStrategy":"validation","validationCode":"let diverged = but_edit_mode::changes_from_initial(&ctx, &perm)\n    .map(|c| !c.is_empty())\n    .unwrap_or(false);\nif diverged { /* prompt user or force */ }","typeGuard":null,"tryCatchPattern":"match abort_and_return_to_workspace(&ctx, false, &mut perm) {\n    Err(e) if e.to_string().contains(\"forced abort is necessary\") => {\n        // confirm with user, then retry with force=true\n        abort_and_return_to_workspace(&ctx, true, &mut perm)?;\n    }\n    other => other?,\n}","preventionTips":["Disable abort actions in the UI when the working tree diverged from the edit-session commit","Persist/checkout pending edit-mode changes before attempting any abort","Never mutate the worktree with external git commands while edit mode is active"],"tags":["git","edit-mode","dirty-worktree","state-guard"],"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"}