An automated git worktree creation and management system that creates worktrees in a structured directory outside your main project.
- π One-command worktree creation:
wt feature-1creates and switches to the worktree - π Organized structure: Worktrees stored in
{project}_worktrees/directory - πΏ Smart branch handling: Works with existing local/remote branches or creates new ones
- β»οΈ Reuse existing worktrees: Navigates to existing worktrees instead of failing
- π‘οΈ Comprehensive error handling: Clear messages for all edge cases
- π§Ή Easy cleanup: Remove worktrees with
wt-removecommand
- Copy the contents of
git-worktree-automation.shto your~/.zshrcfile:
# Add this to the end of your ~/.zshrc file
cat git-worktree-automation.sh >> ~/.zshrc- Reload your shell:
source ~/.zshrc- Place the script in a permanent location:
cp git-worktree-automation.sh ~/.config/git-worktree-automation.sh- Add this line to your
~/.zshrc:
source ~/.config/git-worktree-automation.sh- Reload your shell:
source ~/.zshrcThis method is ideal if you want to keep the script in this repository and easily update it:
- Create a symlink to the script in a directory that's in your PATH or config directory:
# Option A: Link to a directory in your PATH (e.g., /usr/local/bin)
ln -s "$(pwd)/git-worktree-automation.sh" /usr/local/bin/git-worktree-automation.sh
# Option B: Link to your config directory
mkdir -p ~/.config
ln -s "$(pwd)/git-worktree-automation.sh" ~/.config/git-worktree-automation.sh- Add this line to your
~/.zshrc:
# If you used Option A (PATH directory)
source /usr/local/bin/git-worktree-automation.sh
# If you used Option B (config directory)
source ~/.config/git-worktree-automation.sh- Reload your shell:
source ~/.zshrcBenefits of the symlink method:
- β
Easy updates - just
git pullin this repository - β Version control - track changes to your automation script
- β Portability - works across multiple machines with the same setup
- β
Clean separation - keeps your
.zshrcminimal
| Command | Description | Example |
|---|---|---|
wt <branch-name> |
Create/switch to worktree | wt feature-1 |
wt-list |
List all worktrees | wt-list |
wt-remove <branch> |
Remove a worktree | wt-remove feature-1 |
# In project "myproject"
# New branches (created from current HEAD)
wt feature-1 # Creates myproject_worktrees/feature-1
wt bugfix/issue-123 # Creates myproject_worktrees/bugfix/issue-123
wt hotfix-2024 # Creates myproject_worktrees/hotfix-2024
# Remote branches (after git fetch)
wt feature/user-auth # Creates from origin/feature/user-auth
wt release/v2.1.0 # Creates from origin/release/v2.1.0
# Existing local branches
wt main # Creates from local main branch
wt develop # Creates from local develop branch# List all worktrees
wt-list
# Output:
# π Git worktrees for myproject:
#
# π feature-1: /Users/you/projects/myproject_worktrees/feature-1
# πΏ Branch: feature-1
# π Commit: a1b2c3d4
#
# π main: /Users/you/projects/myproject
# πΏ Branch: main
# π Commit: e5f6g7h8
# Remove a worktree
wt-remove feature-1The script has built-in support for remote branches! This is perfect for team collaboration where you need to work on branches created by colleagues or switch between different remote branches quickly.
The script automatically detects and handles remote branches in this priority order:
- Local branch (if exists) -
refs/heads/branch-name - Remote branch (if exists) -
refs/remotes/origin/branch-name - New branch (if neither exists) - Creates from current HEAD
# Colleague pushed "feature/user-authentication" to origin
git fetch # Get latest remote refs
wt feature/user-authentication # Creates worktree tracking origin/feature/user-authentication
# Script output:
# β
Found existing remote branch: origin/feature/user-authentication
# βοΈ Creating worktree at: myproject_worktrees/feature/user-authentication
# β
Worktree created successfully from existing branch!
# π Worktree setup complete!# Work on hotfixes
git fetch
wt hotfix/critical-security-fix # Creates from origin/hotfix/critical-security-fix
# Review pull requests
git fetch
wt feature/new-dashboard # Creates from origin/feature/new-dashboard
# Work on release branches
git fetch
wt release/v2.1.0 # Creates from origin/release/v2.1.0
# Handle complex branch names
git fetch
wt bugfix/issue-1234-payment-gateway # Creates from origin/bugfix/issue-1234-payment-gatewayImportant: Remote branches must be visible locally before the script can use them.
# Always fetch first to see latest remote branches
git fetch
# Or fetch from specific remote
git fetch origin
# Verify remote branch exists
git branch -r | grep feature-name
# Then use the script
wt feature-nameWhen you run wt remote-branch-name:
-
Detection Phase:
# Script checks in order: git show-ref --verify --quiet "refs/heads/remote-branch-name" # Local first git show-ref --verify --quiet "refs/remotes/origin/remote-branch-name" # Then remote
-
Worktree Creation:
# For remote branches, git automatically: git worktree add myproject_worktrees/remote-branch-name remote-branch-name # This creates a local tracking branch automatically
-
Automatic Tracking Setup:
- Creates local
remote-branch-namebranch - Sets up tracking to
origin/remote-branch-name - Ready for commits and pushes
- Creates local
Understanding which branch the script will use:
# Scenario 1: Only remote branch exists
git branch -r | grep feature-x # Shows: origin/feature-x
wt feature-x # β
Uses origin/feature-x
# Scenario 2: Both local and remote exist
git branch | grep feature-y # Shows: feature-y
git branch -r | grep feature-y # Shows: origin/feature-y
wt feature-y # β
Uses local feature-y (priority)
# Scenario 3: Neither exists
wt feature-z # β
Creates new branch from HEADDaily Workflow:
# Morning routine - sync with team
git fetch
wt feature/current-task # Work on your feature
# Switch to review colleague's work
git fetch
wt feature/colleague-task # Quick switch to review
# Switch back to your work
wt feature/current-task # Instantly back to your branchMultiple Remote Scenarios:
# The script currently checks 'origin' remote
# For other remotes, fetch them first:
git fetch upstream
git fetch fork
# Then create local branch manually if needed:
git checkout -b upstream-feature upstream/feature-name
wt upstream-feature # Now script can use local branchBranch not found:
# β Error: Branch 'feature-x' not found
git fetch # Fetch latest remotes
git branch -r | grep feature # Verify branch name
wt feature-x # Try againMultiple remotes conflict:
# If you have branches with same name on different remotes
git branch -r | grep feature-name # See all remote versions
# Create specific local branches:
git checkout -b feature-origin origin/feature-name
git checkout -b feature-upstream upstream/feature-name
# Then use script:
wt feature-origin # Uses your local branchOutdated remote references:
# If remote branch was deleted but you still see it
git remote prune origin # Clean up stale references
git fetch # Get fresh remote refsWhen you run wt feature-1 in a project called "myproject":
parent-directory/
βββ myproject/ # Original project
βββ myproject_worktrees/ # Worktrees directory
βββ feature-1/ # Your new worktree
βββ bugfix-123/ # Another worktree
βββ hotfix-2024/ # Yet another worktree
The script intelligently handles different branch scenarios:
- Existing Local Branch: Creates worktree from local branch
- Existing Remote Branch: Creates worktree tracking the remote branch
- New Branch: Creates worktree with a new branch from current HEAD
- Existing Worktree: Navigates to the existing worktree directory
- β Validates git repository before proceeding
- β Checks for required parameters
- β Handles worktree creation failures gracefully
- β Prevents overwriting existing worktrees
- β Provides clear error messages and suggestions
- β Cleans up empty worktree directories after removal
wt(): Core worktree creation functionwt-list(): Lists all worktrees with detailswt-remove(): Safely removes worktrees
# Gets the git repository root, not current subdirectory
local git_root=$(git rev-parse --show-toplevel)
local current_dir=$(basename "$git_root")# Check local branches
git show-ref --verify --quiet "refs/heads/$branch_name"
# Check remote branches
git show-ref --verify --quiet "refs/remotes/origin/$branch_name"# For existing branches
git worktree add "$worktree_path" "$branch_name"
# For new branches
git worktree add -b "$branch_name" "$worktree_path"Error: "Not in a git repository"
- Make sure you're running the command from within a git repository
- Run
git statusto verify you're in a git repo
Error: "Failed to create worktree"
- The branch might already be checked out in another worktree
- Use
wt-listto see existing worktrees - Try
wt-remove <branch>if the worktree exists but is problematic
Error: "Worktree not found"
- Use
wt-listto see available worktrees - Check if you're in the correct git repository
If something goes wrong, you can manually clean up:
# List all worktrees
git worktree list
# Remove a problematic worktree
git worktree remove /path/to/worktree
# Or force remove if needed
git worktree remove --force /path/to/worktree- Git 2.5+ (for
git worktreesupport) - Bash or Zsh shell
- Standard Unix utilities (basename, dirname, mkdir)
This project is licensed under the MIT License - see the LICENSE file for details.