A TypeScript tool for managing git worktrees with automatic port allocation, environment file setup, and AI agent integration via MCP (Model Context Protocol).
- Easy worktree management - Create, switch, and remove worktrees with simple commands
- Automatic port allocation - Each worktree gets unique API and web ports
- Environment file setup - Automatically copies and configures .env files with correct ports
- Cursor rules syncing - Syncs
.cursor/rules/*.mdcfiles including gitignored ones - MCP server integration - Native AI agent support via Model Context Protocol
- Full terminal compatibility - Works in any terminal, not just IDE-specific
npm install -g git-worktree-managergit clone https://github.com/yourusername/git-worktree-manager.git
cd git-worktree-manager
pnpm install
pnpm build
npm link # Makes 'wt' available globally# List all worktrees
wt list
# Create a new worktree
wt create feature-x
# Set up and open a worktree (installs deps, configures env, opens editor)
wt switch feature-x
# Remove a worktree
wt remove feature-xList all git worktrees with status information.
wt listShows:
- Main worktree location
- All worktrees with branch, path, env status, and dependencies status
Create a new worktree.
wt create feature-x # Creates new branch worktree/feature-x
wt create bugfix main # Checkout existing 'main' branchSet up a worktree and open it in your editor. This command:
- Fixes detached HEAD state (if any)
- Syncs cursor rules from main worktree
- Copies and configures environment files with unique ports
- Installs dependencies (if needed)
- Opens the worktree in your editor
wt switch feature-x
wt switch feature-x --no-editor # Skip opening editorRemove a worktree.
wt remove feature-x
wt remove feature-x --force # Force removalShow a detailed status table of all worktrees.
wt statusCheck what's running on common development ports.
wt portsPrint the path to a worktree (for shell navigation).
cd $(wt cd feature-x)# Editor to open worktrees with
EDITOR_CMD=cursor
# Base ports (worktrees get offset from these)
BASE_API_PORT=4000
BASE_WEB_PORT=5173Same format as global config. Project config overrides global config.
| Setting | Default |
|---|---|
EDITOR_CMD |
cursor |
BASE_API_PORT |
4000 |
BASE_WEB_PORT |
5173 |
Ports are automatically assigned based on worktree position:
| Worktree | API Port | Web Port |
|---|---|---|
| main | 4000 | 5173 |
| worktree 1 | 4001 | 5174 |
| worktree 2 | 4002 | 5175 |
| worktree 3 | 4003 | 5176 |
The tool automatically detects and copies env files from common locations:
.env(root)apps/api/.envapps/web/.envpackages/api/.env
Port variables are automatically substituted:
PORT→ API portAPP_URL→http://localhost:{webPort}VITE_API_BASE→http://localhost:{apiPort}/apiVITE_PORT→ Web port
When switching to a worktree, the tool syncs .cursor/rules/*.mdc files from the main worktree:
- Gitignored files (like
credentials.mdc) are always copied - Non-gitignored files are only copied if they don't exist in the target
This ensures your AI coding assistant has the same rules across all worktrees.
Start the MCP server for AI agent integration:
wt --mcp| Tool | Description |
|---|---|
worktree_list |
List all worktrees with detailed info |
worktree_create |
Create a new worktree |
worktree_remove |
Remove a worktree |
worktree_get |
Get info about a specific worktree |
worktree_ports |
Check port usage status |
Add to your ~/.cursor/mcp.json:
{
"mcpServers": {
"git-worktree-manager": {
"command": "wt",
"args": ["--mcp"]
}
}
}# Install dependencies
pnpm install
# Run in development mode
pnpm dev
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Type check
pnpm typecheck
# Lint
pnpm lint
# Build
pnpm buildsrc/
├── core/ # Core business logic
│ ├── types.ts # TypeScript types
│ ├── errors.ts # Custom error classes
│ ├── config.ts # Configuration loading
│ ├── git.ts # Git operations
│ ├── worktree.ts # Worktree CRUD
│ ├── environment.ts # Env file management
│ ├── ports.ts # Port allocation
│ └── cursor-rules.ts # Cursor rules syncing
├── cli/ # CLI interface
│ ├── index.ts # Main entry point
│ ├── formatter.ts # Output formatting
│ └── commands/ # CLI commands
└── mcp/ # MCP server
└── index.ts # MCP server implementation
MIT