Managing Git Worktrees
Manage git worktrees in the worktrees/ directory for working on multiple branches simultaneously.
Operations
Determine the operation from context or $ARGUMENTS:
- Create: "create", "add", "new worktree", or a branch name
- List: "list", "show", "status"
- Remove: "remove", "delete", "rm"
- Prune: "prune", "clean", "cleanup"
Create Worktree
Create a new worktree for a branch.
Validate git repository:
git rev-parse --git-dirGet branch name from $ARGUMENTS or ask user
Ask if creating new branch or checking out existing
Create worktree:
# Existing branch git worktree add worktrees/<branch-name> <branch-name> # New branch git worktree add -b <branch-name> worktrees/<branch-name>Report path:
worktrees/<branch-name>
List Worktrees
Display all worktrees with status.
Get worktree information:
git worktree list git worktree list --porcelainPresent clearly:
- Path (relative when possible)
- Branch name
- Commit hash and message
- Status (locked, prunable)
Example output:
Main Worktree:
Path: /Users/name/project
Branch: main
Commit: abc1234 Latest changes
Worktrees:
Path: worktrees/feature-x
Branch: feature-x
Commit: def5678 Add feature
Remove Worktree
Remove a worktree and optionally its branch.
List worktrees:
git worktree listGet worktree to remove from $ARGUMENTS or ask user
Confirm removal and ask about branch deletion
Remove worktree:
git worktree remove <path> # Force if uncommitted changes: git worktree remove --force <path>Delete branch if requested:
git branch -d <branch-name> # Force if unpushed: git branch -D <branch-name>
Safety checks:
- Warn about uncommitted changes
- Warn about unpushed commits
- Require confirmation for force operations
- Never remove main worktree
Prune Worktrees
Clean up stale worktree metadata.
Check what would be pruned:
git worktree prune --dry-run --verboseShow current worktrees:
git worktree listExplain findings (stale entries, reasons)
Prune:
git worktree prune --verboseVerify:
git worktree list
When to use:
- After manually deleting worktree directories
- When git shows non-existent worktrees
- When getting "worktree already exists" errors
Safety: Only removes metadata for already-deleted directories. Does not delete worktree directories or branches.
Arguments
$ARGUMENTS