--- name: git-workspace description: "Work with git worktrees and workspaces for concurrent development. Use when: (1) Creating new workspaces with \`git worktree add\`, (2) Listing existing worktrees with \`git worktree list\`, (3) Removing worktrees with \`git worktree remove\`, (4) Pruning stale worktree metadata with \`git worktree prune\`, (5) Managing multiple checked-out branches simultaneously, (6) Migrating uncommitted work between workspaces, or (7) Any git workspace/worktree operation. Requires Git 2.5+ for worktree support." --- # Git Workspace Operations Work with git worktrees to enable concurrent development across multiple branches without context switching or stashing conflicts. ## Prerequisites - Git 2.5+ installed (worktree support introduced in 2.5) - Working in a git repository - Understanding of git branches and basic git operations ## Quick Start **Create a new workspace:** ```bash git worktree add -b [] ``` **List all worktrees:** ```bash git worktree list ``` **Remove a worktree:** ```bash git worktree remove ``` ## Core Operations ### Create Worktree Create a new worktree (workspace) linked to the current repository: ```bash # Create worktree with new branch git worktree add -b [] # Create worktree with existing branch git worktree add # Create worktree at specific commit (detached HEAD) git worktree add --detach ``` **Best practices:** - Use relative paths outside current worktree (e.g., `../workspace-feature-x`) - Branch name should be descriptive and match feature/task - Default to creating new branch unless explicitly checking out existing branch **Example:** ```bash git worktree add -b feature-user-auth ../workspace-user-auth ``` ### List Worktrees List all worktrees associated with the repository: ```bash # Basic list git worktree list # Verbose output (shows branch and commit) git worktree list -v # Machine-readable format (for scripting) git worktree list --porcelain ``` **Output interpretation:** - First line is main worktree (cannot be removed) - Subsequent lines are linked worktrees - Format: ` []` ### Remove Worktree Remove a worktree when done: ```bash # Remove clean worktree (default - safe) git worktree remove # Force remove unclean worktree (discards uncommitted changes) git worktree remove --force ``` **Safety considerations:** - Default behavior only removes "clean" worktrees (no untracked files, no modified tracked files) - Use `--force` only when intentionally discarding uncommitted work - Main worktree cannot be removed - Always verify worktree path before removal **Example:** ```bash git worktree remove ../workspace-feature-x ``` ### Prune Stale Worktrees Clean up administrative files from manually deleted worktrees: ```bash # Dry run (see what would be pruned) git worktree prune -n # Prune stale worktrees git worktree prune # Prune with expiration (remove entries older than specified time) git worktree prune --expire