由来

自从各种AI agent工具发展以来,我的git目录中就出现了一大堆git worktree目录。然后直接删除还提示

error: cannot delete branch 'worktree-agent-a09b851d2f8abc431' used by worktree at 'xxxxxxx/.claude/worktrees/agent-a09b851d2f8abc431'

解决办法

AI 拉的屎只能用 AI去解决

git worktree list --porcelain \
  | awk '/^worktree / {print substr($0, 10)}' \
  | tail -n +2 \
  | while IFS= read -r wt; do
      git worktree remove --force "$wt"
    done

git worktree prune -v

git branch --format='%(refname:short)' \
  | grep '^worktree-agent-' \
  | xargs -r git branch -D

异常情况

有可能因为对应的 worktree 被锁了,无法删除

可以执行 git worktree remove -f -f .claude/worktrees/agent-a66e69abcd25e09fb 强制删除