Worktree CLI
A powerful CLI tool for managing git worktrees with enhanced features including centralized storage, automatic config file synchronization, and intelligent branch management.
Features
- 🗂️ Centralized Storage - Organizes worktrees in
~/.worktrees/<repo-name>/<branch-name>/ - ⚙️ Smart Config Management - Automatically copies gitignored config files to new worktrees
- 🔄 Branch Synchronization - Keeps worktrees and git branches in sync
- 📋 Comprehensive Status - Shows detailed worktree and branch status
- 🎯 Configurable Patterns - Customize which files to copy via
.worktree-config.toml - 🛡️ Safe Branch Names - Automatically sanitizes branch names with slashes and special characters
Installation
1. Build and Install the Binary
# Clone and build the project
# Install the binary (choose one option)
# OR add to your PATH
2. Set Up Shell Integration
The worktree command is a shell function that wraps worktree-bin to enable directory changing and enhanced completions. You need to set this up for your shell:
Bash
Add to your ~/.bashrc or ~/.bash_profile:
# Generate and source worktree shell integration
Zsh
Add to your ~/.zshrc:
# Generate and source worktree shell integration
Fish
Add to your Fish config (~/.config/fish/config.fish):
# Generate and source worktree shell integration
worktree-bin init fish | source
3. Enable Shell Completions (Optional)
For enhanced tab completions, add these to your shell config:
Bash
# Add to ~/.bashrc
Zsh
# Add to ~/.zshrc
Fish
# Add to ~/.config/fish/config.fish
worktree-bin completions fish | source
4. Reload Your Shell
# Reload your shell configuration
# or restart your terminal
Verify Installation
# Test that worktree command is available
# Test shell integration works
Quick Start
# Check current status
# Create a new worktree for feature development
# Create a worktree with a new branch
# List all worktrees
# List worktrees for current repo only
# Remove a worktree
# Remove worktree and delete the branch
Commands
create - Create a new worktree
Options:
-p, --path <PATH>- Custom path for the worktree (optional)-b, --create-branch- Create a new branch if it doesn't exist
Examples:
# Create worktree for existing branch
# Create worktree with new branch
# Create worktree at custom location
list - List all worktrees
Options:
--current- Show worktrees for current repo only
Examples:
# List all managed worktrees
# List worktrees for current repository
remove - Remove a worktree
Options:
-d, --delete-branch- Also delete the associated branch
Examples:
# Remove worktree by branch name
# Remove worktree and delete branch
# Remove worktree by path
status - Show worktree status
Displays comprehensive information about:
- Git worktrees vs managed worktrees
- Directory existence status
- Synchronization state
- Repository information
sync-config - Sync config files between worktrees
Examples:
# Sync config from main to feature branch
# Sync using paths
Configuration
.worktree-config.toml
Create a .worktree-config.toml file in your repository root to customize which files are copied to new worktrees:
[]
= [
".env*",
".vscode/",
"*.local.json",
"config/local/*",
".idea/",
"docker-compose.override.yml"
]
= [
"node_modules/",
"target/",
".git/",
"*.log",
"*.tmp",
"dist/",
"build/"
]
Default patterns (if no config file exists):
Include:
.env*- Environment files.vscode/- VS Code settings*.local.json- Local configuration filesconfig/local/*- Local config directories
Exclude:
node_modules/,target/- Build artifacts.git/- Git directory*.log,*.tmp- Temporary files
Storage Organization
Worktrees are organized in a clean, predictable structure:
~/.worktrees/
├── my-project/
│ ├── main/
│ ├── feature-auth/ # branch: feature/auth
│ ├── bugfix-login/ # branch: bugfix/login
│ └── develop/
├── another-repo/
│ ├── main/
│ └── feature-xyz/ # branch: feature/xyz
└── third-project/
└── experimental/
Branch Name Sanitization: Branch names containing slashes and special characters are automatically sanitized for safe filesystem storage:
feature/auth→feature-auth/bugfix/critical-issue→bugfix-critical-issue/release/v1.0→release-v1.0/
The original branch names are preserved and displayed in all commands.
Use Cases
1. Feature Development
# Start working on a new feature
# Work in the new worktree
# When done, remove it
2. Bug Fixes on Multiple Branches
# Create worktrees for different versions
# Apply fixes to both
# Config files are automatically synced
3. Code Review
# Create temporary worktree for PR review
# Review code without affecting main workspace
# Clean up when done
4. Development Environment Management
# Sync updated config to all worktrees
# Check which worktrees need attention
Troubleshooting
Worktree exists but not in git
# Remove the directory manually and recreate
Config files not copying
- Check
.worktree-config.tomlsyntax - Verify file patterns match your files
- Ensure files aren't excluded by exclude patterns
Permission issues
# Ensure worktree directory is writable
Advanced Usage
Custom Storage Location
Set a custom worktree storage location by modifying the storage module or using environment variables (future enhancement).
Integration with IDEs
The consistent storage structure makes it easy to:
- Configure IDE project templates
- Set up automated workflows
- Create shell aliases for common operations
Shell Aliases
Add these to your shell profile for convenience:
Contributing
- Fork the repository
- Create a feature branch:
worktree create -b feature-name - Make your changes
- Test thoroughly
- Submit a pull request
License
MIT License - see LICENSE file for details.