`wt merge` removing default branch/worktree
Bug Report
Description
wt merge removed the master worktree and branch instead of the feature branch
worktree that was being merged.
Output
◎ Stashing changes in ~/Development/<bare-repo>/tree/feat-sidenav-ai-draft-btn...
◎ Restoring stashed changes in /Development/<bare-repo>/tree/feat-sidenav-ai-draft-btn...
○ Already up to date with feat/sidenav-ai-draft-btn (no new commits, no rebase needed)
◎ Running pre-remove user:tmux
tmux kill-session -t master 2>/dev/null || true
◎ Running pre-remove project:server
docker compose down
◎ Removing master worktree & branch in background (same commit as feat/sidenav-ai-draft-btn, _)
○ Switched to worktree for feat/sidenav-ai-draft-btn @ /Development/<bare-repo>/tree/feat-sidenav-ai-draft-btn
Error: nu::shell::error
× $env.PWD points to a non-existent directory
help: Use `cd` to reset $env.PWD into a good state
Observations
- The pre-remove hook ran with
masteras the branch - confirming the master worktree was the one being removed - The message says
(same commit as feat/sidenav-ai-draft-btn, _)- the commit hash appears as_rather than an actual SHA - The shell error at the end confirms the current working directory was deleted
switch.no-cd = trueis set in the project config
Current Config
USER CONFIG @ ~/.config/worktrunk/config.toml
worktree-path = "{{ repo_path }}/tree/{{ branch | sanitize }}"
[commit.generation]
command = "/nix/store/rp383jwqa60cc7rc0acd2wnlhjfsc5q4-worktrunk-commit"
[list]
summary = false
[merge]
squash = false
[post-create]
tmux = "/nix/store/v6piy0wi2q6krp34z6jpv961v8vw51yp-worktrunk-post-create {{ branch | sanitize }} {{ worktree_path }}"
[pre-remove]
tmux = "tmux kill-session -t {{ branch | sanitize }} 2>/dev/null || true"
[switch]
no-cd = true
↳ Optional system config not found @ /etc/xdg/worktrunk/config.toml
PROJECT CONFIG @ ~/Development/<bare-repo>/tree/master/.config/wt.toml
[pre-commit]
format = "vendor/bin/pint --dirty"
[post-create]
submodules = "git submodule update --init --recursive"
copy = "wt step copy-ignored"
env = '''
grep -q "^APP_SUBDOMAIN=" .env && sed -i "s/^APP_SUBDOMAIN=.*/APP_SUBDOMAIN={{ branch | sanitize }}/
" .env || echo "APP_SUBDOMAIN={{ branch | sanitize }}" >> .env
grep -q "^APP_HOST=" .env && sed -i 's/^APP_HOST=.*/APP_HOST="${APP_SUBDOMAIN}.${APP_DOMAIN}"/' .env
|| echo 'APP_HOST="${APP_SUBDOMAIN}.${APP_DOMAIN}"' >> .env
'''
[pre-remove]
server = "docker compose down"
# ============================================================================
# Dev Server URL (shown in `wt list`)
# ============================================================================
[list]
url = "https://{{ branch | sanitize }}.truncated.test"
Environment
- worktrunk version:
wt 0f0cb2a - git version:
git version 2.52.0 - OS:
nixos
Thanks for reporting this! I was able to reproduce the issue and identified the root cause: in bare repos, all worktrees are linked (is_linked() = true), so the guard that protects the main worktree from removal (in_main) always evaluated to false. This left the default branch worktree unprotected during wt merge.
I've opened #1620 with a fix. A maintainer will review it shortly.
sorry for the issue @viicslen
@worktrunk-bot has this always been the case?
one thing that still I'm confused about: the report says that master was removed instead of the source branch. is that right? that seems like a worse and different problem from the main vs primary worktree. or was the source master is this case?
Has this always been the case?
Yes — since the protection was first introduced in 278dfaa1. The original check used is_in_worktree() which compared git_dir != common_dir. In bare repos, every linked worktree has a distinct git dir (.git/worktrees/<name>), so this returned true for all worktrees — same behavior as is_linked(). The default branch worktree was never protected in bare repos.
Was master removed instead of the source branch?
Master was the source in this case. Looking at the output:
Already up to date with feat/sidenav-ai-draft-btn→ the merge target is the feature branchRemoving master worktree & branch→ master is the current/source worktree being removedSwitched to worktree for feat/sidenav-ai-draft-btn→ switched to the target after removal
The user ran wt merge from the master worktree (targeting the feature branch). In wt merge, the source (current) worktree is always the one removed. Since the bare repo protection was broken, master was treated as a regular removable worktree. So it's the same bug — not a separate problem.
ah OK, that explains why we haven't hit it
Does that mean we now have duplicative checks between merging master into another branch and running wt merge in the default worktree? Should we consolidate those?
Good question — they're actually not duplicative because wt merge and wt remove take different code paths.
wt merge bypasses prepare_worktree_removal() entirely — it constructs the RemoveResult directly (merge.rs:271). So the check_not_default_branch guard in repository_ext.rs:201-202 only protects wt remove, not wt merge.
That means the in_primary check in merge.rs is the sole protection during merge, and the Phase 2/3 checks in prepare_worktree_removal are the sole protection during wt remove. They guard different entry points rather than the same one twice.
That said — the fact that merge constructs RemoveResult directly rather than going through prepare_worktree_removal is what made this bug possible. If merge used the shared removal path, the Phase 3 check_not_default_branch would have caught it independently. Could be worth consolidating so merge goes through the same validation, but that's a bigger refactor since merge needs to do its own dirty-check timing and skip the interactive parts of removal.
ok, do a draft of that consolidation in a separate PR to see whether it's viable
Drafted in #1625. The approach extracts a prepare_merge_removal() method on RepositoryCliExt that runs the same Phase 2 (main-worktree guard) and Phase 3 (default-branch guard) checks as prepare_worktree_removal(), then constructs RemoveResult — replacing the bespoke in_primary logic and direct construction in merge.rs.
It's a relatively clean split: merge still handles its own on_target check before calling the shared method (that's merge-specific), and the command approval gate uses a lightweight check rather than calling the full method. The actual validation and RemoveResult construction are now shared.
What changed
repository_ext.rs: Newprepare_merge_removal()— validates viais_linked()/check_not_default_branch()/ensure_clean(), then buildsRemoveResultmerge.rs: Replacedin_primary+ directRemoveResultconstruction with call to shared method- Two snapshot updates: when merging main→main, now shows "already on target branch" instead of "primary worktree" (both true, but the former is more descriptive of the actual reason)