Manage git worktrees for parallel feature development. Sub-commands: create, sync, cleanup
Create, sync, and clean up git worktrees for isolated feature development.
/worktree create <feature-name> [base-branch]
/worktree sync [base-branch]
/worktree cleanup <feature-name> [--force]
Parse the first argument as the sub-command and dispatch accordingly.
If no sub-command is provided, display:
Usage:
/worktree create <feature-name> [base-branch] — create new isolated worktree
/worktree sync [base-branch] — sync current worktree with base
/worktree cleanup <feature-name> [--force] — remove worktree after merge
Create a new git worktree for isolated feature development with automatic plan setup.
When to use: Starting a new feature that needs multiple commits, working on multiple features in parallel, or keeping the main branch clean during development.
Ensure feature name is provided (second argument). Convert to kebab-case:
FEATURE_NAME=$(echo "$2" | tr '[:upper:]' '[:lower:]' | tr '_' '-' | tr ' ' '-')
Use third argument if provided, otherwise detect main or master from git refs.
Verify this is a git repository. Warn (but allow) if there are uncommitted changes. Fetch latest from origin.
BRANCH_NAME="feature/$FEATURE_NAME"
WORKTREE_PATH="worktrees/$FEATURE_NAME"
mkdir -p worktrees
git worktree add "$WORKTREE_PATH" -b "$BRANCH_NAME" "origin/$BASE_BRANCH"
Create plans/active/$FEATURE_NAME/plan.md with frontmatter template (title, status: planning, worktree, created date) and sections for Overview, Success Criteria, Phases, Notes, and Open Questions.
✅ Worktree created!
Location: worktrees/$FEATURE_NAME
Branch: feature/$FEATURE_NAME
Plan: plans/active/$FEATURE_NAME/plan.md
Next: cd $WORKTREE_PATH, edit the plan, start building
When done: /worktree sync | /worktree cleanup $FEATURE_NAME
Sync the current feature worktree with its base branch.
When to use: Incorporating latest changes from main before a PR, during long-running feature development, or after team members land changes.
Check current directory is a worktree (path contains /worktrees/). Warn if not. Get current branch with git branch --show-current.
Use argument if provided, otherwise detect from remote refs.
If uncommitted changes exist, print status and exit with instructions to commit or stash first.
git fetch origin
BEHIND=$(git rev-list --count HEAD..origin/$BASE_BRANCH)
# If already up to date, exit with success
git rebase "origin/$BASE_BRANCH"
On rebase failure: show conflicting files and instructions to resolve then git rebase --continue, or abort with git rebase --abort.
On success: show summary and next steps (run tests, push with --force-with-lease).
Remove a git worktree and associated files after a feature is merged.
When to use: After a feature branch is merged to main, or when abandoning an experiment.
Require feature name (second argument). Check worktrees/$FEATURE_NAME exists.
git branch -r --merged origin/$BASE_BRANCH | grep feature/$FEATURE_NAMEIf plans/active/$FEATURE_NAME exists, offer to archive it to plans/archived/$FEATURE_NAME-$(date +%Y%m%d).
git worktree remove "$WORKTREE_PATH"
Offer to delete remote branch (git push origin --delete feature/$FEATURE_NAME) and local branch (git branch -d feature/$FEATURE_NAME).
Show what was removed and remaining worktrees via git worktree list.