# clean-worktree > Safely remove a git worktree and its associated branches. Use when done with a feature branch worktree. Prevents shell breakage by ensuring correct CWD. - Author: Michael Edelman - Repository: medelman17/vibedocs - Version: 20260204214045 - Stars: 1 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/medelman17/vibedocs - Web: https://mule.run/skillshub/@@medelman17/vibedocs~clean-worktree:20260204214045 --- --- name: clean-worktree description: Safely remove a git worktree and its associated branches. Use when done with a feature branch worktree. Prevents shell breakage by ensuring correct CWD. disable-model-invocation: true allowed-tools: Bash --- # Clean Worktree Skill Safely remove worktree: $ARGUMENTS ## Pre-flight Checks 1. **Identify the main repo root** (not a worktree): ```bash git rev-parse --path-format=absolute --git-common-dir | sed 's/\/.git$//' ``` 2. **Change to main repo FIRST** (CRITICAL - prevents shell breakage): ```bash cd ``` 3. **List current worktrees** to confirm target exists: ```bash git worktree list ``` ## Workflow 1. **Check if branch was merged** (look for squash merge): ```bash git log main --oneline --grep="" | head -3 # Or check commits not in main: git log main.. --oneline ``` 2. **If merged, proceed with cleanup**: ```bash # Remove worktree git worktree remove # Delete local branch git branch -d # Delete remote branch (if exists) git push origin --delete ``` 3. **If NOT merged, confirm with user** before proceeding: - Show unmerged commits - Ask if they want to merge first or discard 4. **Verify cleanup**: ```bash git worktree list git branch -a | grep ``` ## Safety Rules - **NEVER** run `git worktree remove` while CWD is inside the worktree - **ALWAYS** cd to main repo before any removal commands - **ALWAYS** check merge status before deleting branches - Use `git branch -d` (safe delete) not `-D` (force delete) ## Common Patterns ### Worktree was squash-merged via PR ```bash # Check for squash merge git log main --oneline --grep="" | head -1 # If found, safe to delete everything cd git worktree remove .worktrees/ git branch -d feature/ git push origin --delete feature/ ``` ### Worktree has unmerged work ```bash # Show what would be lost git log main..feature/ --oneline # User must explicitly confirm before using -D ```