Use when managing git worktrees. Provides patterns for creation, cleanup, and branch naming.
Standardized patterns for managing git worktrees across the development workflow. All commands that create or manage worktrees should use these patterns.
Provides single source of truth for worktree operations, branch naming conventions, and cleanup strategies. Ensures consistent worktree management across all workflows and commands.
Convert feature descriptions to standardized branch names:
-)feature/ for new features (default)bugfix/ for bug fixes (if description contains "fix" or "bug")hotfix/ for urgent production fixesrelease/ for release preparationchore/ for maintenance, docs, refactoring, test additions, and other non-feature workNote: branch types are broader than conventional commit types (feat:, fix:, docs:, etc.).
Branches are temporary workspaces, so fewer categories suffice.
| Description | Branch Name |
|---|---|
| "add dark mode toggle" | feature/add-dark-mode-toggle |
| "fix authentication bug" | bugfix/authentication-bug |
| "Update documentation" | chore/update-documentation |
| "refactor API client" | chore/refactor-api-client |
# Generate branch name from description
DESCRIPTION="add dark mode toggle"
PREFIX="feature" # or "bugfix" if description contains "fix" or "bug"; "chore" for docs/refactor/test
BRANCH_NAME=$(echo "$DESCRIPTION" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd 'a-z0-9-')
BRANCH_NAME="${PREFIX}/${BRANCH_NAME}"
Branch names with slashes create nested directories, which is the intended behavior:
# Branch with slash creates nested structure
BRANCH="feature/my-feature"
# Creates: ~/git/repo-name/feature/my-feature/
Note: Slashes in branch names are preserved to maintain 1:1 mapping between branch names and directory structure.
All worktrees follow this structure:
~/git/{REPO_NAME}/{BRANCH_NAME}/
Example:
ai-assistant-instructionsfeature/add-dark-mode~/git/ai-assistant-instructions/feature/add-dark-mode/Get repository name:
REPO_NAME=$(basename $(git rev-parse --show-toplevel))
Find main worktree path:
MAIN_PATH=$(git worktree list | head -1 | awk '{print $1}')
Sync main branch:
cd "$MAIN_PATH"
git switch main
git fetch --all --prune --force
git pull
Create worktree:
WORKTREE_PATH=~/git/${REPO_NAME}/${BRANCH_NAME}
mkdir -p "$(dirname "$WORKTREE_PATH")"
git worktree add "$WORKTREE_PATH" -b "$BRANCH_NAME" main
Verify:
cd "$WORKTREE_PATH"
git status
Worktrees are stale if:
Branch is merged into main:
git branch --merged main | grep -q "^ $BRANCH$"
Remote branch deleted:
git branch -r | grep -q "origin/$BRANCH"
# If exit code is 1, remote branch is gone
Branch shows as [gone]:
git branch -vv | grep "\[gone\]"
List all worktrees:
git worktree list
For each non-main worktree, check if stale using above criteria
Remove stale worktree:
git worktree remove "$WORKTREE_PATH"
Delete local branch (if merged or gone):
git branch -d "$BRANCH_NAME" # Safe delete (merged only)
# or
git branch -D "$BRANCH_NAME" # Force delete
Prune administrative files:
git worktree prune
Before creating worktrees or after cleanup, sync main:
# Find main worktree
MAIN_PATH=$(git worktree list | head -1 | awk '{print $1}')
cd "$MAIN_PATH"
# Ensure on main branch
git switch main
# Fetch and prune
git fetch --all --prune --force
# Pull latest
git pull
# Return to original directory
cd -
WORKTREE_PATH=~/git/repo-name/feat_branch-name
if [ -d "$WORKTREE_PATH" ]; then
echo "Worktree already exists at $WORKTREE_PATH"
cd "$WORKTREE_PATH"
else
echo "Creating new worktree..."
# Use creation pattern above
fi
git worktree list
# Output format:
# /path/to/main abc123 [main]
# /path/to/worktree-1 def456 [feature/feature-1]
# /path/to/worktree-2 ghi789 [bugfix/bug-fix]
CURRENT_BRANCH=$(git branch --show-current)
git rev-parse --is-inside-work-tree
# Exit code 0 = valid git repo
# Exit code != 0 = not a git repo
/refresh-repo - Sync main, check PR status, cleanup stale worktrees/create-worktrees:create-worktrees - Create worktrees for all open PRs (plugin)/sync-main - Syncs main across worktreesFor creating worktrees for all open PRs, use the create-worktrees plugin instead of manual creation:
/create-worktrees:create-worktrees
This plugin automatically:
Cause: Worktree directory exists but not registered with git
Solution:
# Remove the directory
rm -rf "$WORKTREE_PATH"
# Re-create using proper git worktree add command
Cause: Branch name contains invalid characters
Solution: Use branch naming convention from this skill (alphanumeric and hyphens only)
Cause: Worktree directory was deleted without using git worktree remove
Solution:
git worktree prune
Cause: Uncommitted changes in worktree
Solution:
# Commit or stash changes first
cd "$WORKTREE_PATH"
git stash
cd -
# Then remove
git worktree remove "$WORKTREE_PATH"
feature/my-feature creates ~/git/repo/feature/my-feature/)git worktree prune after removing worktrees