Worktree CLI
A powerful CLI tool that transforms git worktree management from painful to effortless. Stop juggling multiple local repos, losing config files, or manually organizing worktree directories.
The Problem: Git worktrees are incredibly useful for parallel development, but the native git commands are cumbersome. You end up with worktrees scattered across your filesystem, config files that don't transfer, and no easy way to navigate between them.
The Solution: Worktree CLI provides a centralized, intelligent system that handles all the complexity for you. Create, manage, and navigate worktrees with simple commands while automatically maintaining your development environment.
Why Use Worktree CLI?
- โก Zero Setup Friction - Create worktrees anywhere in your project with one command
- ๐ Centralized & Organized - All worktrees live in
~/.worktrees/<repo>/<branch>/- no more scattered directories - ๐ Config Sync Magic - Your
.env,.vscode/, and local config files automatically follow you to new worktrees - ๐งญ Effortless Navigation - Jump between worktrees instantly with smart completions and interactive selection
- ๐ Quick Return - Navigate back to original repo from any worktree with
worktree back - ๐งน Self-Cleaning - Automatically cleans up orphaned branches and references to prevent git clutter
- ๐ง Developer-Friendly - Shell integration with intelligent tab completions and directory changing
- ๐ก๏ธ Handles Edge Cases - Safe branch name sanitization, git config inheritance, and sync state management
Installation
โ ๏ธ Important: This tool requires shell integration to function properly. The
worktree jumpandworktree backcommands won't work without it, and you'll miss out on intelligent tab completions. Make sure to complete both installation steps below.
1. Install from crates.io
# Install the latest version from crates.io
This will install the worktree-bin binary to your cargo bin directory (typically ~/.cargo/bin/). Make sure this directory is in your PATH.
2. Set Up Shell Integration with Completions
Important: The worktree command is a shell function that wraps worktree-bin to enable directory changing and provides enhanced tab completions automatically. Without this integration, worktree jump and worktree back won't be able to change your current directory.
Add the following to your shell configuration:
Bash
Add to your ~/.bashrc or ~/.bash_profile:
# Generate and source worktree shell integration with completions
Zsh
Add to your ~/.zshrc:
# Generate and source worktree shell integration with completions
Fish
Add to your Fish config (~/.config/fish/config.fish):
# Generate and source worktree shell integration with completions
worktree-bin init fish | source
Note: The shell integration provides sophisticated tab completions that enhance your workflow:
Command & Flag Completion:
- All subcommands (
create,list,jump, etc.) with intelligent suggestions - All flags and options with descriptions (powered by clap)
- Context-aware completion based on your current command
Dynamic Worktree Completion:
- Live completion of worktree names for
worktree jump - Fuzzy matching - type partial names and get suggestions
- Pressing TAB on empty
worktree jumptriggers interactive selection - Completion respects
--currentflag to show only current repository worktrees
Smart Navigation:
- Shell integration enables
worktree jumpandworktree backto actually change directories - All other commands are delegated to the binary while maintaining completion support
3. 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 what's currently set up
# Create a worktree for an existing branch
# Create a worktree with a brand new branch
# Jump between worktrees instantly
# Use interactive selection when you can't remember the name
# Navigate back to the original repo from any worktree
# See all your worktrees
# Clean up when branches get out of sync
# Remove a worktree when you're done (deletes branch by default)
# Keep the branch but remove the worktree
Commands
| Command | Description | Key Options |
|---|---|---|
create <branch> |
Create a new worktree | --new-branch, --existing-branch |
list |
List all worktrees | --current |
remove <target> |
Remove a worktree | --keep-branch |
status |
Show worktree status | - |
sync-config <from> <to> |
Sync config files between worktrees | - |
jump [target] |
Navigate to a worktree directory | --interactive, --current |
back |
Navigate back to original repository | - |
cleanup |
Clean up orphaned branches and references | - |
completions <shell> |
Generate shell completions | - |
init <shell> |
Generate shell integration | - |
create - Create a new worktree
Options:
--new-branch- Force creation of a new branch (fail if it already exists)--existing-branch- Only use an existing branch (fail if it doesn't exist)
Examples:
# Create worktree for existing branch
# Create worktree with new branch
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:
--keep-branch- Keep the branch (only remove the worktree, branch deleted by default)
Examples:
# Remove worktree by branch name
# Remove worktree (deletes branch by default)
# Remove worktree but keep the branch
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
jump - Navigate to a worktree directory
Options:
--interactive- Launch interactive selection mode--current- Show worktrees for current repo only
Examples:
# Jump to a specific worktree
# Interactive selection (also triggered by pressing TAB on empty jump)
# Jump with tab completion - type partial name and press TAB
# Current repo worktrees only
cleanup - Clean up orphaned branches and worktree references
Automatically cleans up your workspace by:
- Removing git branches that have no corresponding worktree directory
- Cleaning up branch mappings for non-existent worktrees
- Removing git worktree references that point to non-existent directories
This command is useful when worktrees get out of sync due to manual deletion or filesystem issues.
Examples:
# Clean up orphaned branches and references
back - Navigate back to the original repository
Navigates back to the original repository directory that the current worktree was created from. This command only works when executed from within a worktree directory that was created using worktree create.
Examples:
# From within a worktree, return to the original repo
completions - Generate shell completions
Generates native shell completions for the specified shell. This is separate from the integrated completions provided by worktree init.
Options:
<SHELL>- Shell to generate completions for (bash, zsh, fish)
Examples:
# Generate completions for bash
# Generate completions for zsh
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 Workflow
# Start working on a new feature (from main repo)
# Jump to the new worktree instantly
# Your .env, .vscode/, and config files are already there!
# Work on your feature...
# Jump back to main when needed
# When done, remove it (deletes branch by default)
2. Parallel Development
# Work on multiple features simultaneously
# Jump between them effortlessly
# Or use interactive selection
3. Code Review & Testing
# Create temporary worktree for PR review
# Jump to review (config already synced)
# Test the changes, then return to your work
# Clean up when done
4. Maintenance & Cleanup
# Regular maintenance - clean up orphaned branches
# Check what's currently active
# List all your worktrees across projects
Troubleshooting
Worktree Commands Don't Change Directory
Problem: worktree jump or worktree back doesn't change your current directory.
Solution: You need to set up shell integration. The binary alone cannot change the shell's directory.
# Add to your shell profile (.bashrc, .zshrc, etc.)
Sync Issues Between Git and Filesystem
Problem: worktree status shows inconsistent state between git worktrees and directories.
Solution: Use the cleanup command to automatically fix sync issues.
Tab Completion Not Working
Problem: Tab completion for worktree jump doesn't show worktree names.
Solutions:
- Ensure shell integration is set up (see above)
- Check that
worktree-binis in your PATH - Restart your shell after setup
Config Files Not Copying
Problem: Your .env or config files aren't appearing in new worktrees.
Solutions:
- Check
.worktree-config.tomlsyntax in your repo root - Verify file patterns match your files (use
*for wildcards) - Ensure files aren't excluded by exclude patterns
- Check that source files exist and aren't gitignored
# Debug: see what patterns are being used
# Check if files copied to ~/.worktrees/repo/test-config/
Permission Issues
Problem: Cannot create or access worktree directories.
Solutions:
# Ensure worktree directory is writable
# Check disk space
# Verify directory ownership
Back Navigation Not Working
Problem: worktree back says no origin information available.
Solution: This affects worktrees created before the back feature was added.
# Recreate the worktree to enable back navigation
Advanced Usage
Git Configuration Inheritance
When creating a new worktree, the tool automatically inherits git configuration from the parent repository. This includes:
- User name and email settings
- Custom git aliases and configurations
- Repository-specific settings
- Credential helpers and authentication settings
This ensures that your git workflow remains consistent across all worktrees without manual configuration.
Origin Tracking and Back Navigation
Each worktree stores metadata about its origin repository, enabling seamless navigation:
- Automatic origin tracking: When creating a worktree, the tool stores the path to the original repository
- Smart back navigation: Use
worktree backfrom any worktree to return to the original repo - Cross-platform paths: Handles path canonicalization and symlinks correctly (e.g.,
/varโ/private/varon macOS)
This feature is particularly useful when working with multiple projects or when you need to quickly return to the main repository.
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 --new-branch feature-name - Make your changes
- Test thoroughly
- Submit a pull request
License
MIT License - see LICENSE file for details.