Syntax
Arguments
This command takes no arguments.Behavior
Theclean command performs batch cleanup with safety protections:
1. Repository and Default Branch Detection
- Locates the repository root
- Accesses the bare repository at
.bare/ - Detects the default branch (main/master) automatically
functions/worktree.zsh:179-183
2. Worktree Discovery
Retrieves all worktrees using porcelain format:- Queries Git for complete worktree list
- Parses worktree information line by line
- Processes each worktree for potential removal
functions/worktree.zsh:185-187
3. Worktree Filtering
For each worktree, applies protection rules: Skip Bare Repository- The
.bare/directory itself is a “worktree” in Git’s view - Never attempts to remove the bare repository
- Continues to next worktree
functions/worktree.zsh:190-192
Skip Default Branch
- Identifies worktree by directory basename
- If basename matches default branch name, skips it
- Ensures default branch worktree is always preserved
functions/worktree.zsh:193-196
4. Removal Attempt
For worktrees not skipped:- Displays message indicating removal attempt
- Executes
git worktree removeon the path - Suppresses error output to keep display clean
functions/worktree.zsh:197-198
5. Error Handling
If removal fails:- Displays warning (does not abort the command)
- Indicates the worktree may have uncommitted changes
- Continues processing remaining worktrees
functions/worktree.zsh:199-201
6. Cleanup and Verification
After processing all worktrees:- Runs
git worktree pruneto clean up stale references - Displays remaining worktrees with
repo wt list - Shows final state of the repository
functions/worktree.zsh:205-208
Examples
Clean All Feature Branches
functions/worktree.zsh:197-208
Clean with Protected Worktrees
functions/worktree.zsh:199-201
Post-Sprint Cleanup
Verify Before Cleaning
What Gets Removed?
Theclean command removes:
- All feature branch worktrees
- All bugfix branch worktrees
- PR review worktrees
- Any non-default branch worktrees
- The bare repository (
.bare/) - The default branch worktree (main/master)
- Worktrees with uncommitted changes (protected by Git)
Error Cases
Repository Root Not Found
functions/worktree.zsh:179-180
Worktrees with Uncommitted Changes
- Commit changes:
cd <worktree> && git commit -am "Save work" - Stash changes:
cd <worktree> && git stash - Remove manually with force:
repo wt rm <branch>then force remove if needed
functions/worktree.zsh:199-201
No Worktrees to Clean
If only the default branch exists:Tips and Best Practices
When to Use Clean
Ideal times to clean worktrees:- After completing a sprint or milestone
- After merging multiple feature branches
- Before starting new work
- When switching between major project phases
- Periodically to free up disk space
Safe by Default
The command is designed to be safe:- Cannot remove default branch
- Cannot remove worktrees with uncommitted work
- Continues on errors rather than aborting
- Shows what remains after cleaning
Commit Before Cleaning
For maximum cleanup, ensure all worktrees have clean state:Selective Removal
If you want to keep some worktrees:Default Branch Detection
The command automatically detects:mainormasteras default- Uses Git’s
HEADreference on origin - Respects your repository’s convention
functions/worktree.zsh:182-183
Post-Clean Actions
After cleaning, the command:- Runs
git worktree pruneto remove stale administrative data - Displays remaining worktrees for verification
- Ensures your repository state is clean and consistent
functions/worktree.zsh:205-208
Disk Space Recovery
Cleaning worktrees frees disk space:- Removes working directory files
- Keeps Git history (in bare repo)
- Each worktree can be several hundred MB
- Useful for large codebases
Integration with Git Workflow
Common workflow pattern:Manual Override
If you need to force remove a worktree with changes:Verification
Always verify the clean results:Alternative to Clean
For more control:See Also
- repo wt rm - Remove a specific worktree
- repo wt list - View all worktrees
- repo wt add - Create new worktrees
- Worktree Overview - Understanding worktrees