Syntax
Arguments
<branch>(required): The branch name for the worktree
Behavior
Theadd command creates a new worktree with intelligent branch handling:
1. Check for Existing Worktree
If a worktree already exists for the branch:- Displays a message that the worktree exists
- Runs post-add hooks (e.g., directory navigation)
- Returns successfully without modification
2. Fetch Latest Changes
Before creating the worktree:- Fetches from
originto ensure branch information is up-to-date - Uses the bare repository configuration at
.bare/
3. Branch Resolution
The command checks for branches in this priority order: Remote Branch (highest priority)- If
origin/<branch>exists, creates worktree from remote branch - Sets up tracking relationship:
branch -> origin/branch - Automatically pulls latest changes from remote
- If
<branch>exists locally but not remotely, uses local branch - No tracking relationship is set
- If branch doesn’t exist locally or remotely, creates a new branch
- Branches off from the default branch (main/master)
- Does not set up remote tracking initially
4. Worktree Location
Worktrees are created at:<repo-root>/<branch>/
For example, if your repo is at /Users/dev/my-repo and you add branch feature-x:
- Worktree path:
/Users/dev/my-repo/feature-x/
5. Post-Add Hooks
After successful creation, thepost_wt_add hook is called with the worktree path, which may:
- Navigate to the worktree directory
- Run environment setup commands
- Display custom messages
Examples
Create Worktree from Remote Branch
functions/worktree.zsh:39-43
Create Worktree for New Branch
functions/worktree.zsh:47-49
Create Worktree from Local Branch
functions/worktree.zsh:44-45
Error Cases
Missing Branch Name
functions/worktree.zsh:21-25
Repository Root Not Found
functions/worktree.zsh:29
Git Worktree Creation Failed
- The branch name contains invalid characters
- Disk space is insufficient
- File permissions prevent directory creation
functions/worktree.zsh:52-55
Tips and Best Practices
Branch Naming
- Use clear, descriptive branch names:
feature/,fix/,refactor/prefixes - Avoid special characters that may cause filesystem issues
- Branch names become directory names, so keep them filesystem-friendly
Workflow Integration
Handling Existing Worktrees
If you runadd on an existing worktree:
- No error occurs
- Worktree is not recreated
- Post-add hooks still run (useful for navigation)
functions/worktree.zsh:33-37
Remote Tracking
When adding a branch that exists remotely:- Tracking is automatically configured
git pullandgit pushwork without additional setup- You can immediately start working with remote changes
Starting from Default Branch
New branches are created from your repository’s default branch:- Usually
mainormaster - Automatically detected by Repo Manager
- Ensures consistent starting point for new work
functions/worktree.zsh:47-49
See Also
- repo wt list - View all worktrees
- repo wt go - Navigate to a worktree
- repo wt rm - Remove a worktree
- repo wt pr - Create worktree from pull request