worktree-detection
marmelab/atomic-crm/.claude/skills/worktree-detection/SKILL.md
How to find a Git worktree by task number without hardcoding paths. Use in hooks or scripts that need to locate a specific task's worktree.
Skill1.3k starsChanged 3 months ago
What's in it
- Worktree Detection
- When to Use
- Pattern
- Why this works
- Extracting TASKNUM from agent variables
- Red Flags
- Verification
---
name: worktree-detection
description: How to find a Git worktree by task number without hardcoding paths. Use in hooks or scripts that need to locate a specific task's worktree.
---
# Worktree Detection
Reference snippet for locating a task's Git worktree from its task number,
without hardcoding paths.
## When to Use
- Inside a hook or script that must act on a specific task's worktree.
- Any time you'd otherwise hardcode a `/tmp/...` worktree path.
## Pattern
```bash
TASK_NUM="TASK-005"
WORKTREE=$(git worktree list --porcelain \
| grep "^worktree " \
| awk '{print $2}' \
| grep -i "${TASK_NUM}" \
| head -1)
if [ -z "$WORKTREE" ] || [ ! -d "$WORKTREE" ]; then
echo "No worktree found for ${TASK_NUM} — skipping"
exit 0
fi
```
## Why this works
`git worktree list` reads from Git's internal registry — it knows every worktree regardless of where it was created. No assumptions about directory structure or machine-specific paths.
## Extracting TASK_NUM from agent variables
```bash
TASK_NUM=$(echo "$TASK_OWNER $TASK_SUBJECT" | grep -oP 'TASK-\d+' | head -1)
```
Works whether the task number appears in the owner field or the subject line.
## Red Flags
- A hardcoded `/tmp/...` worktree path instead of querying `git worktree list`.
- Assuming a directory layout rather than reading Git's registry.
- Treating a missing worktree as an error instead of skipping cleanly (`exit 0`).
## Verification
- [ ] Worktree resolved via `git worktree list --porcelain`, never a hardcoded path.
- [ ] A missing/absent worktree is handled (skip, don't crash).
More agent context in marmelab/atomic-crm
19 other files this repository gives its agents.
AGENTS.md
CLAUDE.md
Skill
- adr-writing.claude/skills/adr-writing/SKILL.md
- backend-dev.claude/skills/backend-dev/SKILL.md
- delete-initial-resource.claude/skills/delete-initial-resource/SKILL.md
- e2e-conventions.claude/skills/e2e-conventions/SKILL.md
- frontend-dev.claude/skills/frontend-dev/SKILL.md
- grill-me.claude/skills/grill-me/SKILL.md
- playwright-testing.claude/skills/playwright-testing/SKILL.md
- ponytail-audit.claude/skills/ponytail-audit/SKILL.md
- ponytail-debt.claude/skills/ponytail-debt/SKILL.md
- ponytail-help.claude/skills/ponytail-help/SKILL.md
- ponytail-review.claude/skills/ponytail-review/SKILL.md
- ponytail.claude/skills/ponytail/SKILL.md
- resolving-rollback-conflicts.claude/skills/resolving-rollback-conflicts/SKILL.md
- setup-interview.claude/skills/setup-interview/SKILL.md
- shadcn-customization.claude/skills/shadcn-customization/SKILL.md
- update-branding.claude/skills/update-branding/SKILL.md
- writing-migrations.claude/skills/writing-migrations/SKILL.md
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
No reports yet. Be the first to say whether it worked.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool registry_write, action report. How to connect one.

