gitbutlerapp/gitbutler · error

The working tree differs from the original commit. A forced…

Error message

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.

What it means

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.

Solutions

  1. Call abort_and_return_to_workspace with force=true if you truly want to discard the divergence (the changes will be lost).
  2. Before aborting, save/commit or stash the working-tree changes through the normal edit-mode exit path so nothing is lost.
  3. Inspect the divergence with the starting_index_state/changes_from_initial API to confirm what differs before forcing.
  4. Fix the caller (UI) to disable/force-guard the abort action when the tree is dirty.

Example fix

// before
abort_and_return_to_workspace(&ctx, false, &mut perm)?;
// after
let dirty = changes_from_initial(&ctx, &perm).map(|c| !c.is_empty()).unwrap_or(false);
abort_and_return_to_workspace(&ctx, dirty, &mut perm)?; // force only when needed
Defensive patterns

Strategy: validation

Validate before calling

let diverged = but_edit_mode::changes_from_initial(&ctx, &perm)
    .map(|c| !c.is_empty())
    .unwrap_or(false);
if diverged { /* prompt user or force */ }

Try / catch

match abort_and_return_to_workspace(&ctx, false, &mut perm) {
    Err(e) if e.to_string().contains("forced abort is necessary") => {
        // confirm with user, then retry with force=true
        abort_and_return_to_workspace(&ctx, true, &mut perm)?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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/37ffcdd4d960326f. Report an issue: GitHub.

Appendix: source

Thrown at crates/gitbutler-edit-mode/src/lib.rs:281

    for ref_name in [EDIT_BRANCH_REF, UNCOMMITTED_CHANGES_REF] {
        // Tolerate an already-absent ref, but surface real lookup failures.
        if let Some(reference) = repo.try_find_reference(ref_name)? {
            reference
                .delete()
                .with_context(|| format!("Failed to delete reference {ref_name}"))?;
        }
    }
    delete_edit_mode_metadata(ctx)?;
    Ok(())
}

pub(crate) fn abort_and_return_to_workspace(
    ctx: &Context,
    force: bool,
    perm: &mut RepoExclusive,
) -> Result<()> {
    if !force && !changes_from_initial(ctx, perm.read_permission())?.is_empty() {
        bail!(
            "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."
        );
    }

    #[expect(deprecated, reason = "checkout/materialization boundary")]
    let repo = &*ctx.git2_repo.get()?;

    // Checkout gitbutler workspace branch
    repo.set_head(WORKSPACE_BRANCH_REF)
        .context("Failed to set head reference")?;

    let uncommited_changes = get_uncommitted_changes(&*ctx.repo.get()?)?;
    let uncommited_changes = repo.find_tree(uncommited_changes.to_git2())?;

    repo.checkout_tree(
        uncommited_changes.as_object(),
        Some(CheckoutBuilder::new().force().remove_untracked(true)),
    )?;

View on GitHub (pinned to 58e5313667)