wt remove should refuse to remove the default branch worktree
Problem
wt remove -D (and wt remove in general) allows removing the default branch worktree (e.g. main/master). This can happen in two ways:
- Running wt remove -D while inside the main worktree (implicit target)
- Running wt remove -D main explicitly
In both cases, removing the default branch worktree is almost certainly unintended — it's the base worktree that all others branch from.
Proposal
wt remove should refuse to remove the default branch worktree by default. If someone truly needs to do this, a flag like --allow-default could override the protection.
This would be similar to how git branch -d main refuses to delete the currently checked-out branch — a simple guardrail against a destructive mistake.
Thanks for the suggestion! I searched the codebase and confirmed this protection doesn't currently exist.
There is an existing guard that prevents removing the main worktree (the original git directory from git clone/git init) — see repository_ext.rs L170-174. But that only checks is_linked(), so in bare repo setups where the default branch worktree is a linked worktree, there's no protection against accidentally removing it.
The proposed guardrail — refusing to remove the default branch worktree unless an explicit override flag is passed — would be a natural extension of the existing main-worktree protection.
I'll leave it for a maintainer to evaluate and prioritize.
@cperalt is this specifically about bare repos?
@max-sixty yes i believe so, my setup is bare repo, where main is its own tree, i accidentally was in main and i did wt remove on it
@worktrunk-bot what does git do when removing the main worktree when it's not linked?
(doesn't mean we should necessarily follow though...)
Git distinguishes between the main worktree (the original directory from git clone/git init) and linked worktrees (created via git worktree add).
For the main worktree, git refuses unconditionally — even --force doesn't help:
$ git worktree remove .
fatal: '.' is a main working tree
$ git worktree remove --force .
fatal: '.' is a main working tree
For linked worktrees (the bare-repo case from this issue), git has no concept of a "default branch worktree" — it treats all linked worktrees equally. So git worktree remove ../main succeeds silently, removing the worktree and its directory without any warning.
That's the gap: in a bare repo setup, the default branch worktree is linked (not main), so git's built-in protection doesn't apply. Worktrunk's existing is_linked() guard (repository_ext.rs L170-174) mirrors git's behavior, but doesn't cover this case either.
does worktrunk remove the default branch with wt remove in a bare repo? does it evaluate main as "consistent with main and then remove it?
Yes — wt remove main does an exact branch-name match against git worktree list output. In repository_ext.rs L90-92:
worktrees
.iter()
.find(|wt| wt.branch.as_deref() == Some(branch))
So wt remove main finds the worktree whose branch is exactly main, and since it's a linked worktree in a bare repo, the is_linked() guard at L172 passes, and it gets removed.
For the implicit case (wt remove with no args while inside the main worktree), it resolves via the current working directory path (L142-166), finds the matching worktree, and same outcome — is_linked() is true, so it proceeds.
Both paths converge at the same is_linked() check, which only protects the main worktree (the original git directory), not the default branch's linked worktree in a bare repo.
but does it remove the branch, not just the worktree?
Yes — by default, wt remove attempts to delete the branch too. The behavior depends on flags:
- Default (
wt remove main): removes the worktree, then tries to delete the branch withgit branch -Donly if it's fully integrated into the target (a safe-delete check viaintegration_reason()). Sincemainis the default branch and the target, it would find it's trivially integrated (same commit) and delete it. -D(wt remove -D main): removes the worktree and force-deletes the branch unconditionally.--no-delete-branch: removes the worktree but keeps the branch.
The branch deletion logic is in branch_deletion.rs — it runs git branch -D when the branch is integrated or when force-delete is set.
So in the bare repo case from this issue: wt remove main would remove both the worktree directory and the main branch itself (since main is trivially integrated into itself).
Default (
wt remove main): removes the worktree, then tries to delete the branch withgit branch -Donly if it's fully integrated into the target (a safe-delete check viaintegration_reason()). Sincemainis the default branch and the target, it would find it's trivially integrated (same commit) and delete it.
OK, let's fix this — main is never "grayed out because it's integrated into main" itself. Let's use that logic to define what to delete — why is it not grayed out in wt list, but it is deleted when we run wt remove
Good framing — the inconsistency is clear in the code.
wt list explicitly skips the integration check for the default branch in item.rs L594:
if is_main || default_branch.is_none() {
return None; // never gray out the default branch
}
But wt remove doesn't have that guard. branch_deletion.rs L37 calls integration_reason() unconditionally, and since main is trivially at the same commit as itself, compute_integration_lazy() returns SameCommit — so it looks "integrated" and gets deleted.
The fix would be to apply the same principle in delete_branch_if_safe: if the branch being removed is the default branch, it's never "integrated" in the meaningful sense, so skip the deletion (or require an explicit override). That would align wt remove's behavior with wt list's — the default branch is the target, not something that integrates into itself.