From c63b9cceb4a664d563e681d94a782ddca98991c5 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Thu, 21 May 2026 14:50:15 +0100 Subject: [PATCH] Make worktree rm actually work in the presence of submodules Also adds the `--force` option to remove a worktree with unstaged changes. --- worktree/worktree.plugin.zsh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/worktree/worktree.plugin.zsh b/worktree/worktree.plugin.zsh index 22d31ab..923e22d 100644 --- a/worktree/worktree.plugin.zsh +++ b/worktree/worktree.plugin.zsh @@ -102,7 +102,22 @@ Manage git worktrees by branch name." fi local -a rm_args=() (( force )) && rm_args+=(--force) - if ! git worktree remove "${rm_args[@]}" "$wt_path"; then + local err + if err=$(git worktree remove "${rm_args[@]}" "$wt_path" 2>&1); then + : + elif [[ "$err" == *"working trees containing submodules"* ]]; then + if (( ! force )) && \ + [[ -n "$(git -C "$wt_path" status --porcelain --ignore-submodules=all 2>/dev/null)" ]]; then + print -P "%F{red}error:%f worktree has uncommitted changes (use --force): $wt_path" + failed=1 + continue + fi + if ! rm -rf "$wt_path" || ! git worktree prune; then + failed=1 + continue + fi + else + print -- "$err" >&2 failed=1 continue fi