Crates.io | worktree |
lib.rs | worktree |
version | 0.2.0 |
created_at | 2025-08-29 17:36:42.441336+00 |
updated_at | 2025-08-29 23:05:19.865888+00 |
description | A powerful CLI tool for managing git worktrees with enhanced features including centralized storage, automatic config file synchronization, and intelligent branch management |
homepage | https://github.com/cafreeman/worktree |
repository | https://github.com/cafreeman/worktree |
max_upload_size | |
id | 1816202 |
size | 149,425 |
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.
~/.worktrees/<repo>/<branch>/
- no more scattered directories.env
, .vscode/
, and local config files automatically follow you to new worktreesworktree back
โ ๏ธ Important: This tool requires shell integration to function properly. The
worktree jump
andworktree back
commands won't work without it, and you'll miss out on intelligent tab completions. Make sure to complete both installation steps below.
# Install the latest version from crates.io
cargo install worktree
This will install the worktree-bin
binary to your cargo bin directory (typically ~/.cargo/bin/
). Make sure this directory is in your PATH.
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:
Add to your ~/.bashrc
or ~/.bash_profile
:
# Generate and source worktree shell integration with completions
eval "$(worktree-bin init bash)"
Add to your ~/.zshrc
:
# Generate and source worktree shell integration with completions
eval "$(worktree-bin init zsh)"
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:
create
, list
, jump
, etc.) with intelligent suggestionsDynamic Worktree Completion:
worktree jump
worktree jump
triggers interactive selection--current
flag to show only current repository worktreesSmart Navigation:
worktree jump
and worktree back
to actually change directories# Reload your shell configuration
source ~/.bashrc # for bash
source ~/.zshrc # for zsh
# or restart your terminal
# Test that worktree command is available
worktree --help
# Test shell integration works
worktree status
# Check what's currently set up
worktree status
# Create a worktree for an existing branch
worktree create feature/auth
# Create a worktree with a brand new branch
worktree create --new-branch feature/payments
# Jump between worktrees instantly
worktree jump feature/auth
# Use interactive selection when you can't remember the name
worktree jump --interactive
# Navigate back to the original repo from any worktree
worktree back
# See all your worktrees
worktree list
# Clean up when branches get out of sync
worktree cleanup
# Remove a worktree when you're done (deletes branch by default)
worktree remove feature/auth
# Keep the branch but remove the worktree
worktree remove feature/payments --keep-branch
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 worktreeworktree create <branch> [OPTIONS]
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
worktree create feature/login
# Create worktree with new branch
worktree create --new-branch feature/new-thing
list
- List all worktreesworktree list [OPTIONS]
Options:
--current
- Show worktrees for current repo onlyExamples:
# List all managed worktrees
worktree list
# List worktrees for current repository
worktree list --current
remove
- Remove a worktreeworktree remove <target> [OPTIONS]
Options:
--keep-branch
- Keep the branch (only remove the worktree, branch deleted by default)Examples:
# Remove worktree by branch name
worktree remove feature/auth
# Remove worktree (deletes branch by default)
worktree remove feature/auth
# Remove worktree but keep the branch
worktree remove feature/auth --keep-branch
status
- Show worktree statusworktree status
Displays comprehensive information about:
sync-config
- Sync config files between worktreesworktree sync-config <from> <to>
Examples:
# Sync config from main to feature branch
worktree sync-config main feature/auth
# Sync using paths
worktree sync-config ~/.worktrees/project/main ~/.worktrees/project/feature
jump
- Navigate to a worktree directoryworktree jump [target] [OPTIONS]
Options:
--interactive
- Launch interactive selection mode--current
- Show worktrees for current repo onlyExamples:
# Jump to a specific worktree
worktree jump feature/auth
# Interactive selection (also triggered by pressing TAB on empty jump)
worktree jump --interactive
# Jump with tab completion - type partial name and press TAB
worktree jump feat<TAB> # completes to available worktrees
# Current repo worktrees only
worktree jump --current
cleanup
- Clean up orphaned branches and worktree referencesworktree cleanup
Automatically cleans up your workspace by:
This command is useful when worktrees get out of sync due to manual deletion or filesystem issues.
Examples:
# Clean up orphaned branches and references
worktree cleanup
back
- Navigate back to the original repositoryworktree back
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
worktree back
completions
- Generate shell completionsworktree completions <SHELL>
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
worktree completions bash > /usr/local/etc/bash_completion.d/worktree
# Generate completions for zsh
worktree completions zsh > ~/.local/share/zsh/site-functions/_worktree
.worktree-config.toml
Create a .worktree-config.toml
file in your repository root to customize which files are copied to new worktrees:
[copy-patterns]
include = [
".env*",
".vscode/",
"*.local.json",
"config/local/*",
".idea/",
"docker-compose.override.yml"
]
exclude = [
"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 directoriesExclude:
node_modules/
, target/
- Build artifacts.git/
- Git directory*.log
, *.tmp
- Temporary filesWorktrees 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.
# Start working on a new feature (from main repo)
worktree create --new-branch feature/payments
# Jump to the new worktree instantly
worktree jump feature/payments
# Your .env, .vscode/, and config files are already there!
# Work on your feature...
# Jump back to main when needed
worktree back
# When done, remove it (deletes branch by default)
worktree remove feature/payments
# Work on multiple features simultaneously
worktree create --new-branch feature/auth
worktree create --new-branch feature/dashboard
worktree create bugfix/critical-issue
# Jump between them effortlessly
worktree jump auth # Tab completion works!
worktree jump dashboard
worktree jump critical
# Or use interactive selection
worktree jump --interactive
# Create temporary worktree for PR review
worktree create pr-123
# Jump to review (config already synced)
worktree jump pr-123
# Test the changes, then return to your work
worktree back
# Clean up when done
worktree remove pr-123
# Regular maintenance - clean up orphaned branches
worktree cleanup
# Check what's currently active
worktree status
# List all your worktrees across projects
worktree list
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.)
eval "$(worktree-bin init bash)" # or zsh/fish
source ~/.bashrc # reload your shell
Problem: worktree status
shows inconsistent state between git worktrees and directories.
Solution: Use the cleanup command to automatically fix sync issues.
worktree cleanup # Removes orphaned branches and references
worktree status # Verify everything is clean
Problem: Tab completion for worktree jump
doesn't show worktree names.
Solutions:
worktree-bin
is in your PATHProblem: Your .env
or config files aren't appearing in new worktrees.
Solutions:
.worktree-config.toml
syntax in your repo root*
for wildcards)# Debug: see what patterns are being used
worktree create --new-branch test-config
# Check if files copied to ~/.worktrees/repo/test-config/
Problem: Cannot create or access worktree directories.
Solutions:
# Ensure worktree directory is writable
chmod -R u+w ~/.worktrees/
# Check disk space
df -h ~/.worktrees/
# Verify directory ownership
ls -la ~/.worktrees/
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
worktree remove old-worktree --keep-branch
worktree create old-worktree
When creating a new worktree, the tool automatically inherits git configuration from the parent repository. This includes:
This ensures that your git workflow remains consistent across all worktrees without manual configuration.
Each worktree stores metadata about its origin repository, enabling seamless navigation:
worktree back
from any worktree to return to the original repo/var
โ /private/var
on macOS)This feature is particularly useful when working with multiple projects or when you need to quickly return to the main repository.
The consistent storage structure makes it easy to:
Add these to your shell profile for convenience:
alias wt='worktree'
alias wtc='worktree create'
alias wtl='worktree list --current'
alias wts='worktree status'
worktree create --new-branch feature-name
MIT License - see LICENSE file for details.