worktree-cleanup
Corrects the order of operations for removing Git branches linked to active worktrees.
Install
mkdir -p .claude/skills/worktree-cleanup && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/16324" && unzip -o skill.zip -d .claude/skills/worktree-cleanup && rm skill.zipInstalls to .claude/skills/worktree-cleanup
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Enforce the correct order when removing a branch that's pinned by a git worktree: `git worktree remove --force <path>` THEN `git branch -D <branch>`. Trying to delete the branch first fails with `error: cannot delete branch X used by worktree at Y`. Trigger this skill when: - User says "remove branch", "delete branch", "cleanup worktree", "prune branches". - `gh pr merge --delete-branch` errors with `worktree` in the message. - `git branch -D` errors with `used by worktree`. - After merging a PR that has an associated worktree.Key capabilities
- →Detect the branch to clean up from user messages or PRs
- →List worktrees associated with a specified branch
- →Remove a git worktree forcefully if it exists
- →Delete a git branch after its worktree is removed
- →Report on removed branches and pruned worktrees
- →Clean up all merged branches that have associated worktrees
How it works
The skill detects a branch for cleanup, lists associated worktrees, and then executes 'git worktree remove --force <path>' followed by 'git branch -D <branch>'. If no worktree is found, it directly deletes the branch.
Inputs & outputs
When to use worktree-cleanup
- →Cleaning up stale git branches
- →Removing git worktrees safely
- →Batch deleting merged branches
About this skill
The rule
Sequence is ALWAYS:
git worktree remove --force <path> # step 1
git branch -D <branch> # step 2
If you skip step 1, step 2 fails. gh pr merge --delete-branch behaves like
step 2 — it also fails when a worktree pins the branch.
Behavior
- Detect the branch to clean up (from user's message or the PR).
- List worktrees touching the branch:
git worktree list --porcelain | grep -B2 "branch refs/heads/<branch>" - If a worktree exists at path
<path>:- Show it to the user.
- Run
git worktree remove --force <path>(the--forcehandles dirty state in the worktree; warn user if there are uncommitted changes). - THEN run
git branch -D <branch>.
- If no worktree exists for the branch:
- Just
git branch -D <branch>.
- Just
- Report what was removed.
Multiple worktrees on one branch
Impossible under git (each worktree pins its own unique branch). If detection
shows multiple, that's a stale entry — clean with git worktree prune first,
then retry.
Batch cleanup
If user asks "cleanup all merged branches with worktrees":
git branch --merged dev(ormain) for merged branches.- For each: check worktree via
git worktree list --porcelain. - Run sequence per branch.
- Report tally: N branches removed, M worktrees pruned.
Do not
- Never
git branch -Dbeforeworktree remove— always error, waste of round-trip. - Never
git worktree remove <path>without--forceunless the worktree is spotless — the flag is standard in Nimbus flow. - Never delete a branch with unmerged commits without warning the user + showing them the commits that would be lost.
- Never touch
main,dev,master— those are protected default branches.
Cross-repo cleanup
If user asks "cleanup old branches across all my repos", iterate:
for r in ~/Development/cidca/cidca-platform ~/Development/cidca/cidca-services \
~/Development/horus/horus-v2 ~/Development/zotea; do
cd "$r" && echo "=== $(basename $r) ===" && git branch --merged | grep -v -E "(main|dev|master|\*)"
done
Then present the list and let user choose which to remove.
When not to use it
- →When the user is requesting to delete a branch without first removing its worktree
- →When the user wants to remove a worktree without using the --force flag
- →When attempting to delete a protected branch like main, dev, or master
Limitations
- →Cannot delete a branch with unmerged commits without user warning
- →Does not touch protected default branches like main, dev, or master
- →Cannot handle multiple worktrees on one branch, requiring pruning stale entries first
How it compares
This skill automates the two-step process of removing a git worktree and then deleting its branch, preventing errors that occur when the branch is deleted first.
Compared to similar skills
worktree-cleanup side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| worktree-cleanup (this skill) | 0 | 1mo | No flags | Beginner |
| resolve-conflicts | 81 | 8mo | Review | Intermediate |
| openspec-onboard | 10 | 6mo | Review | Beginner |
| codex-cli-bridge | 9 | 9mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
resolve-conflicts
antinomyhq
Use this skill immediately when the user mentions merge conflicts that need to be resolved. Do not attempt to resolve conflicts directly - invoke this skill first. This skill specializes in providing a structured framework for merging imports, tests, lock files (regeneration), configuration files, and handling deleted-but-modified files with backup and analysis.
openspec-onboard
studyzy
Guided onboarding for OpenSpec - walk through a complete workflow cycle with narration and real codebase work.
codex-cli-bridge
alirezarezvani
Bridge between Claude Code and OpenAI Codex CLI - generates AGENTS.md from CLAUDE.md, provides Codex CLI execution helpers, and enables seamless interoperability between both tools
skill-sync
KyleKing
Syncs Claude Skills with other AI coding tools like Cursor, Copilot, and Codeium by creating cross-references and shared knowledge bases. Invoke when user wants to leverage skills across multiple tools or create unified AI context.
github-workflow-automation
ruvnet
Advanced GitHub Actions workflow automation with AI swarm coordination, intelligent CI/CD pipelines, and comprehensive repository management
git-advanced-workflows
wshobson
Master advanced Git workflows including rebasing, cherry-picking, bisect, worktrees, and reflog to maintain clean history and recover from any situation. Use when managing complex Git histories, collaborating on feature branches, or troubleshooting repository issues.