Skip to content

Commit 91b45cc

Browse files
authored
chore(skills): add worktree support to git-branch-cleanup skill (#72)
1 parent 15961de commit 91b45cc

File tree

1 file changed

+51
-0
lines changed
  • .skills/experimental/git-branch-cleanup

1 file changed

+51
-0
lines changed

.skills/experimental/git-branch-cleanup/SKILL.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: |
55
staleness, and remote tracking. Provides interactive selection with safety guards.
66
Use when the user wants to clean up branches, delete old branches, organize Git branches,
77
or asks about which branches can be safely deleted.
8+
compatibility: Requires git 2.17+ (for worktree support). Git 2.22+ recommended for `git branch --show-current`.
89
---
910

1011
# Git Branch Cleanup
@@ -53,6 +54,12 @@ git branch --no-merged "$BASE_BRANCH"
5354
# Branches whose upstream is gone (remote deleted)
5455
git branch -vv | grep -F ': gone]' || true
5556

57+
# List worktrees (branches with '+' prefix in git branch -v have worktrees)
58+
git worktree list
59+
60+
# Branches with worktrees ('+' prefix indicates worktree association)
61+
git branch -v | grep '^+' || true
62+
5663
# Branches ahead of upstream (have unpushed commits)
5764
git for-each-ref refs/heads \
5865
--format='%(refname:short) %(upstream:trackshort)' \
@@ -68,6 +75,7 @@ Categorize branches by safety level:
6875
| **Merged (Safe)** | Already merged to base branch | Safe to delete |
6976
| **Gone Remote** | Remote branch deleted | Review recommended |
7077
| **Stale** | No commits for 30+ days | Needs review |
78+
| **Has Worktree** | Branch checked out in a worktree (`+` prefix in `git branch -v`) | Remove worktree first |
7179
| **Ahead of Upstream** | Has unpushed commits | ⚠️ Do not delete |
7280
| **Unmerged** | Active work in progress | Use caution |
7381

@@ -128,6 +136,35 @@ git branch -d <branch-name> # For merged branches
128136
git branch -D <branch-name> # Force delete (unmerged)
129137
```
130138

139+
**For branches with worktrees**, remove the worktree first:
140+
141+
```bash
142+
# Check if branch has a worktree
143+
git worktree list | grep "\\[$branch\\]"
144+
145+
# Remove worktree before deleting branch
146+
git worktree remove --force /path/to/worktree
147+
git branch -D <branch-name>
148+
```
149+
150+
**Bulk delete [gone] branches with worktree handling:**
151+
152+
```bash
153+
# Process all [gone] branches, handling worktrees automatically
154+
git branch -v | grep '\[gone\]' | sed 's/^[+* ]//' | awk '{print $1}' | while read branch; do
155+
echo "Processing branch: $branch"
156+
# Find and remove worktree if it exists
157+
worktree=$(git worktree list | grep "\\[$branch\\]" | awk '{print $1}')
158+
if [ ! -z "$worktree" ] && [ "$worktree" != "$(git rev-parse --show-toplevel)" ]; then
159+
echo " Removing worktree: $worktree"
160+
git worktree remove --force "$worktree"
161+
fi
162+
# Delete the branch
163+
echo " Deleting branch: $branch"
164+
git branch -D "$branch"
165+
done
166+
```
167+
131168
## Commands Reference
132169

133170
```bash
@@ -154,6 +191,12 @@ git fetch --prune || echo "Warning: Could not reach remote. Using cached data."
154191
# List oldest branches (30+ days)
155192
git for-each-ref --sort=committerdate refs/heads/ \
156193
--format='%(committerdate:short) %(refname:short)' | head -20
194+
195+
# Worktree commands
196+
git worktree list # List all worktrees
197+
git worktree remove /path/to/worktree # Remove a worktree
198+
git worktree remove --force /path/to/worktree # Force remove (even if dirty)
199+
git worktree prune # Clean up stale worktree refs
157200
```
158201

159202
## Dry Run Mode
@@ -181,6 +224,7 @@ Before deletion, verify:
181224
- [ ] Not the current branch
182225
- [ ] Not a protected branch (base/main/master/trunk/develop)
183226
- [ ] No unpushed commits (ahead of upstream)
227+
- [ ] Worktree removed first (if branch has associated worktree)
184228
- [ ] User confirmation obtained
185229

186230
### Quick safety commands
@@ -194,4 +238,11 @@ git for-each-ref refs/heads \
194238
# For a specific branch, confirm it's not ahead of upstream (if upstream exists)
195239
# (Empty output => nothing unpushed)
196240
git log --oneline @{u}..HEAD 2>/dev/null || echo "No upstream configured for this branch"
241+
242+
# Check if a branch has an associated worktree
243+
# ('+' prefix in git branch -v output indicates worktree)
244+
git branch -v | grep "^+" | awk '{print $2}'
245+
246+
# Find worktree path for a specific branch
247+
git worktree list | grep "\\[branch-name\\]"
197248
```

0 commit comments

Comments
 (0)