{"record":{"id":"7141062691833bea","repo":"gitbutlerapp/gitbutler","slug":"refusing-to-check-out-conflicted-commit-new-head","errorCode":null,"errorMessage":"Refusing to check out conflicted commit {new_head_id}","messagePattern":"Refusing to check out conflicted commit (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-core/src/worktree/checkout/function.rs","lineNumber":47,"sourceCode":"/// If this changes, then the source sid of a rename could also cause conflicts, maybe? It's a bit unclear what it would mean\n/// in practice, but I guess that we bring deleted files back instead of conflicting.\n#[instrument(skip(repo), err(Debug))]\npub fn safe_checkout_from_head(\n    new_head_id: gix::ObjectId,\n    repo: &gix::Repository,\n    Options {\n        skip_head_update,\n        merge_base_override,\n        allow_conflicted_commit_checkout,\n        allow_uncommitted_changes_to_conflict_with_new_head,\n    }: Options,\n) -> anyhow::Result<Outcome> {\n    let new_object = new_head_id.attach(repo).object()?;\n    if !allow_conflicted_commit_checkout\n        && new_object.kind.is_commit()\n        && crate::Commit::from_id(new_head_id.attach(repo))?.is_conflicted()\n    {\n        bail!(\"Refusing to check out conflicted commit {new_head_id}\");\n    }\n\n    let git2_repo = git2::Repository::open(repo.git_dir())?;\n    let head_tree_id = repo.head_tree_id_or_empty()?;\n    let head_tree = git2_repo.find_tree(head_tree_id.to_git2())?;\n    let old_tree = if let Some(id) = merge_base_override {\n        let mut opts = git2::DiffOptions::new();\n        opts.context_lines(1);\n        // Also write the index.\n        let tree = git2_repo.find_object(id.to_git2(), None)?.peel_to_tree()?;\n        let diff = git2_repo.diff_tree_to_tree(Some(&head_tree), Some(&tree), Some(&mut opts))?;\n        if git2_repo\n            .apply(&diff, git2::ApplyLocation::Index, None)\n            .is_err()\n        {\n            // Just overwrite the index.\n            git2_repo.index()?.read_tree(&tree)?;\n        }","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-core/src/worktree/checkout/function.rs#L29-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Resolve the conflicts in the commit's files, remove the conflict markers, and amend or create a new commit before checking out","If intentionally checking out a conflicted commit (e.g. tooling or tests), call with allow_conflicted_commit_checkout set to true","Inspect the commit with but's conflict APIs (crate::Commit::from_id(...).is_conflicted()) to identify which files/branches are conflicted","Reset to a clean base commit and re-apply changes without the conflicted hunks"],"exampleFix":"// before\nsafe_checkout_from_head(repo, conflicted_head_id, false, None, ...)?;\n// after\nlet commit = crate::Commit::from_id(conflicted_head_id.attach(repo)?)?;\nif commit.is_conflicted()? {\n    // resolve conflicts first, then amend\n}\nsafe_checkout_from_head(repo, clean_head_id, false, None, ...)?;","handlingStrategy":"validation","validationCode":"let commit = crate::Commit::from_id(new_head_id.attach(&repo)?)?;\nif !allow && commit.is_conflicted()? {\n    // resolve or opt-in before calling safe_checkout_from_head\n}","typeGuard":"fn is_conflicted_commit(repo: &gix::Repository, id: gix::Id<'_>) -> anyhow::Result<bool> {\n    Ok(crate::Commit::from_id(id.attach(repo))?.is_conflicted()?)\n}","tryCatchPattern":"match safe_checkout_from_head(&repo, id, false, None, &mut platform) {\n    Err(e) if e.to_string().contains(\"Refusing to check out conflicted commit\") => resolve_conflicts_then_retry(),\n    other => other?,\n}","preventionTips":["Check is_conflicted() on the target commit before any checkout","Never `git add` files containing conflict markers; resolve first","Enable pre-commit conflict-marker checks in your workflow","For tooling that must handle conflicted commits, pass allow_conflicted_commit_checkout=true explicitly"],"tags":["git","worktree","conflict","checkout"],"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"}