{"record":{"id":"332d0579394cc939","repo":"GitoxideLabs/gitoxide","slug":"cannot-delete-because-another-worktree-has-it-c","errorCode":null,"errorMessage":"cannot delete {} because another worktree has it checked out","messagePattern":"cannot delete (.+?) because another worktree has it checked out","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gix-tix/src/edit/rebase.rs","lineNumber":2913,"sourceCode":"    for worktree_repo in repos {\n        if !seen.insert(worktree_repo.git_dir().to_owned()) {\n            continue;\n        }\n        let Ok(head) = worktree_repo.head() else {\n            continue;\n        };\n        let Some(old) = head.id().map(gix::Id::detach) else {\n            continue;\n        };\n        let planned = head.referent_name().and_then(|name| {\n            expected_refs.and_then(|refs| refs.iter().find(|expected| expected.name.as_bstr() == name.as_bstr()))\n        });\n        let new = match planned {\n            Some(expected) if expected.new.is_none() => {\n                if worktree_repo.git_dir() == repo.git_dir() {\n                    continue;\n                }\n                anyhow::bail!(\n                    \"cannot delete {} because another worktree has it checked out\",\n                    expected.name.shorten()\n                );\n            }\n            Some(expected) => Some(expected.new),\n            None => rewritten.get(&old).copied(),\n        };\n        let Some(new) = new else { continue };\n        if worktree_repo.workdir().is_none() && worktree_repo.is_bare() {\n            continue;\n        }\n        let old_tree = worktree_repo.find_commit(old)?.tree_id()?.detach();\n        let new_tree = match new {\n            Some(new) => repo.find_commit(new)?.tree_id()?.detach(),\n            None if worktree_repo.head()?.referent_name().is_some() => repo.empty_tree().id,\n            None => anyhow::bail!(\"a detached checked-out root commit cannot be removed\"),\n        };\n        if old_tree == new_tree {","sourceCodeStart":2895,"sourceCodeEnd":2931,"githubUrl":"https://github.com/GitoxideLabs/gitoxide/blob/e73179060badf27222d790981fac3f84c1830a7e/gix-tix/src/edit/rebase.rs#L2895-L2931","documentation":"During branch cleanup after a rebase, a rewritten branch that is planned for deletion (`expected.new.is_none()`) is found checked out in a different worktree. Deleting a branch referenced by another worktree's HEAD is not allowed, so the operation bails naming the branch via `expected.name.shorten()`. If it's checked out in the *same* repo/git-dir it is silently skipped (`continue`).","triggerScenarios":"Running a rebase edit that rewrites and deletes branches while a separate linked worktree has `HEAD` pointed at one of the to-be-deleted old branch names — i.e. `worktree_repo.git_dir() != repo.git_dir()` and the planned new branch is `None`.","commonSituations":"Multi-worktree setups where an old feature branch checked out in a second worktree gets rewritten/deleted by a rebase run from the main worktree; CI or scripts operating on the same repository from multiple worktrees.","solutions":["Remove the other worktree (`git worktree remove <path>`) or switch its HEAD off the branch, then retry.","Detach the other worktree's HEAD so the branch is no longer checked out there.","Skip deletion for that branch: keep the old branch instead of planning `new: None` for it.","Run the rebase from (or after consolidating) the worktree that actually owns the branch."],"exampleFix":"// before\nworktree::remove(repo, other_path)?; // branch still checked out -> bail\n// after\ndetach_head_or_switch(other_worktree, \"main\")?; // release the branch\nworktree::remove(repo, other_path)?;\nedit.rebase(...)?; // deletion now succeeds","handlingStrategy":"validation","validationCode":"for wt in repo.worktrees()? {\n    let wt_repo = wt.into_repo()?;\n    if wt_repo.git_dir() != repo.git_dir()\n        && let Some(head_ref) = wt_repo.head()?.referent_name()\n        && branches_planned_for_deletion.contains(&head_ref.to_owned()) {\n        // detach or remove this worktree first\n    }\n}\n","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep branch deletions out of rebase plans while other worktrees exist","Detach secondary worktrees when running history rewrites","Inventory `git worktree list` before automated history edits"],"tags":["worktree","branch-deletion","rebase","git"],"backgroundTag":"resource-in-use","analyzedSha":"e73179060badf27222d790981fac3f84c1830a7e","analyzedAt":"2026-09-08T11:26:50.865Z","contentChangedAt":"2026-09-08T11:26:50.865Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}