gitbutlerapp/gitbutler · error

Refusing to check out conflicted commit

Error message

Refusing to check out conflicted commit {new_head_id}

What it means

safe_checkout_from_head refuses to check out a commit whose tree contains unresolved merge-conflict markers recorded in GitButler's conflict database. Checking out such a commit would silently overwrite or destroy conflict state that the user has not resolved, so the operation bails before touching the worktree. Callers can explicitly opt in by setting allow_conflicted_commit_checkout=true.

Solutions

  1. Resolve the conflicts in the commit's files, remove the conflict markers, and amend or create a new commit before checking out
  2. If intentionally checking out a conflicted commit (e.g. tooling or tests), call with allow_conflicted_commit_checkout set to true
  3. Inspect the commit with but's conflict APIs (crate::Commit::from_id(...).is_conflicted()) to identify which files/branches are conflicted
  4. Reset to a clean base commit and re-apply changes without the conflicted hunks

Example fix

// before
safe_checkout_from_head(repo, conflicted_head_id, false, None, ...)?;
// after
let commit = crate::Commit::from_id(conflicted_head_id.attach(repo)?)?;
if commit.is_conflicted()? {
    // resolve conflicts first, then amend
}
safe_checkout_from_head(repo, clean_head_id, false, None, ...)?;
Defensive patterns

Strategy: validation

Validate before calling

let commit = crate::Commit::from_id(new_head_id.attach(&repo)?)?;
if !allow && commit.is_conflicted()? {
    // resolve or opt-in before calling safe_checkout_from_head
}

Type guard

fn is_conflicted_commit(repo: &gix::Repository, id: gix::Id<'_>) -> anyhow::Result<bool> {
    Ok(crate::Commit::from_id(id.attach(repo))?.is_conflicted()?)
}

Try / catch

match safe_checkout_from_head(&repo, id, false, None, &mut platform) {
    Err(e) if e.to_string().contains("Refusing to check out conflicted commit") => resolve_conflicts_then_retry(),
    other => other?,
}

Prevention

When it happens

Trigger: Calling safe_checkout_from_head with a new_head_id pointing to a commit that crate::Commit::is_conflicted() reports as conflicted, while the allow_conflicted_commit_checkout flag is false. Public callers include commit_file, branch_checkout_with_perm_only, update_unborn_head, and the test entries listed.

Common situations: A user commits files with unresolved conflict markers (e.g. after a merge/rebase left <<<<<<< markers and they ran git add anyway), or an automated flow commits conflicted hunks and then attempts a workspace checkout or HEAD update.

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/7141062691833bea. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-core/src/worktree/checkout/function.rs:47

/// If this changes, then the source sid of a rename could also cause conflicts, maybe? It's a bit unclear what it would mean
/// in practice, but I guess that we bring deleted files back instead of conflicting.
#[instrument(skip(repo), err(Debug))]
pub fn safe_checkout_from_head(
    new_head_id: gix::ObjectId,
    repo: &gix::Repository,
    Options {
        skip_head_update,
        merge_base_override,
        allow_conflicted_commit_checkout,
        allow_uncommitted_changes_to_conflict_with_new_head,
    }: Options,
) -> anyhow::Result<Outcome> {
    let new_object = new_head_id.attach(repo).object()?;
    if !allow_conflicted_commit_checkout
        && new_object.kind.is_commit()
        && crate::Commit::from_id(new_head_id.attach(repo))?.is_conflicted()
    {
        bail!("Refusing to check out conflicted commit {new_head_id}");
    }

    let git2_repo = git2::Repository::open(repo.git_dir())?;
    let head_tree_id = repo.head_tree_id_or_empty()?;
    let head_tree = git2_repo.find_tree(head_tree_id.to_git2())?;
    let old_tree = if let Some(id) = merge_base_override {
        let mut opts = git2::DiffOptions::new();
        opts.context_lines(1);
        // Also write the index.
        let tree = git2_repo.find_object(id.to_git2(), None)?.peel_to_tree()?;
        let diff = git2_repo.diff_tree_to_tree(Some(&head_tree), Some(&tree), Some(&mut opts))?;
        if git2_repo
            .apply(&diff, git2::ApplyLocation::Index, None)
            .is_err()
        {
            // Just overwrite the index.
            git2_repo.index()?.read_tree(&tree)?;
        }

View on GitHub (pinned to 58e5313667)