Agent skill
worktree
Manages git worktrees for parallel development. Creates isolated working directories for tickets/branches, lists active worktrees, and cleans up after merging. Use when starting work on a ticket or cleaning up completed work.
Install this agent skill to your Project
npx add-skill https://github.com/josh-gree/my-claude-skills/tree/main/skills/worktree
SKILL.md
Worktree Management
Purpose
Manage git worktrees to enable parallel development. Worktrees let you work on multiple branches simultaneously without stashing or switching — each branch gets its own directory.
Concepts
What is a worktree?
A worktree is a linked working directory that shares the same .git repository. You can have multiple worktrees, each checked out to a different branch.
Directory structure
Worktrees are created in a .worktrees/ directory alongside the main repo:
~/projects/
my-project/ <- main worktree (on main branch)
.worktrees/
my-project-42-feature/ <- linked worktree
my-project-57-bugfix/ <- linked worktree
All worktrees share the same git history but have independent working directories.
Naming convention
Worktree directories follow the pattern:
<repo-name>-<branch-name>
Example: If the repo is api-service and branch is 42-rate-limiting:
../.worktrees/api-service-42-rate-limiting/
Commands
Create a worktree
Usage: /worktree create [branch-name]
Creates a new worktree for the specified branch. If the branch doesn't exist, creates it from origin/main.
Workflow
-
Verify environment
bashgit remote -v git fetch originEnsure we have a remote and fresh refs.
-
Determine repo name
bashbasename $(git rev-parse --show-toplevel) -
Ensure .worktrees directory exists
bashmkdir -p ../.worktrees -
Check branch doesn't already exist as worktree
bashgit worktree listIf branch already has a worktree, report its location instead.
-
Create the worktree
If branch exists remotely:
bashgit worktree add ../.worktrees/<repo>-<branch> <branch>If branch is new:
bashgit worktree add ../.worktrees/<repo>-<branch> -b <branch> origin/main -
Report the path Tell the user:
- Full path to the new worktree
- How to navigate there:
cd ../.worktrees/<worktree-dir> - Remind them to return to main repo when done
List worktrees
Usage: /worktree list
Shows all active worktrees for this repository.
git worktree list
Output shows:
- Path to each worktree
- Current commit
- Branch name
Clean up a worktree
Usage: /worktree clean [worktree-path-or-branch]
Removes a worktree after work is complete (typically after PR is merged).
Workflow
-
List current worktrees
bashgit worktree list -
Identify which to remove User provides path or branch name. If ambiguous, ask.
-
Check for uncommitted changes
bashcd <worktree-path> git status --porcelainIf changes exist: STOP and warn user. Ask if they want to:
- Commit the changes first
- Discard changes and proceed (requires
--force) - Cancel
-
Check if branch is merged
bashgit branch --merged main | grep <branch-name>If not merged, warn user but allow proceeding if they confirm.
-
Remove the worktree
bashgit worktree remove <worktree-path>Or with force if user confirmed:
bashgit worktree remove --force <worktree-path> -
Optionally delete the branch Ask user if they also want to delete the branch:
bashgit branch -d <branch-name> -
Report success Confirm removal and list remaining worktrees.
Clean up all merged worktrees
Usage: /worktree clean-merged
Removes all worktrees whose branches have been merged to main.
Workflow
-
List worktrees
bashgit worktree list -
For each non-main worktree, check if merged
bashgit branch --merged main -
Show user what will be removed List the merged worktrees and ask for confirmation.
-
Remove each confirmed worktree
bashgit worktree remove <path> git branch -d <branch> -
Report results Show what was removed and what remains.
Safety Rules
- Never force-remove without explicit user confirmation
- Always check for uncommitted changes before removal
- Warn if branch is not merged before removal
- Never delete main/master worktree
- Never delete branches that aren't fully merged without user confirmation
Common Patterns
Starting work on a ticket
# From main repo
/worktree create 42-add-rate-limiting
# Output:
# Created worktree at /Users/you/projects/.worktrees/api-service-42-add-rate-limiting
# Navigate there with: cd ../.worktrees/api-service-42-add-rate-limiting
Checking what you're working on
/worktree list
# Output:
# /Users/you/projects/api-service abc1234 [main]
# /Users/you/projects/.worktrees/api-service-42-add-rate-limiting def5678 [42-add-rate-limiting]
After PR is merged
/worktree clean 42-add-rate-limiting
# Output:
# Removed worktree: /Users/you/projects/.worktrees/api-service-42-add-rate-limiting
# Deleted branch: 42-add-rate-limiting
#
# Remaining worktrees:
# /Users/you/projects/api-service abc1234 [main]
Troubleshooting
"fatal: '' is already checked out"
The branch is already checked out in another worktree. Use /worktree list to find it.
"worktree is dirty"
There are uncommitted changes. Commit or stash them before removing.
Branch not found
Ensure you've fetched: git fetch origin. If it's a new branch, use the create command which will create it from main.
Checklist
Creating
- Fetch latest from origin
- Ensure ../.worktrees directory exists
- Verify branch doesn't already have a worktree
- Create worktree with correct naming convention
- Report path and navigation instructions
Cleaning
- Check for uncommitted changes
- Check if branch is merged
- Get user confirmation if needed
- Remove worktree
- Optionally delete branch
- Report remaining worktrees
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
implement-ticket
Implements a planned GitHub issue by following the implementation plan. Takes a GitHub issue number. Use when the user wants to implement a ticket, work on an issue, or says "implement ticket X" or "do issue
address-pr-comments
Addresses PR review comments by making code changes and posting replies. Takes a PR number or auto-detects from current branch. Use when the user wants to address review feedback, respond to PR comments, fix PR feedback, or says "address comments on PR X".
create-ticket
Creates GitHub issues to capture work that needs to be done. Records intent with context about the codebase, not implementation plans. Use when the user wants to track work, create a ticket, log a task, or says "we want to do X" or "we need to X".
review
Review code using Google's code review principles. Use for ad-hoc file review or as the foundation for PR and codebase reviews.
setup-python-lib
Sets up a new Python library project using uv in the current directory. Creates project structure, installs dev dependencies, and optionally configures git remote. Use when the user wants to initialise a Python library, package, or project with uv.
refine-plan
Refines an existing implementation plan through discussion with the user. Takes a GitHub issue number. Use when the user wants to refine a plan, discuss a plan, change a plan, or says "refine plan X" or "let's discuss the plan for issue
Didn't find tool you were looking for?