Syntax
Arguments
<branch>(required): The branch name of the worktree to remove
Behavior
Therm command safely removes worktrees with built-in protections:
1. Branch Name Resolution
- Takes the branch name as input
- Constructs the worktree path:
<repo-root>/<branch>/ - Prepares for validation and removal
functions/worktree.zsh:74-77
2. Default Branch Protection
Before attempting removal:- Detects the repository’s default branch (main/master)
- Prevents removal of the default branch worktree
- Displays an error if attempted
functions/worktree.zsh:79-84
3. Worktree Existence Check
Validates that the worktree exists:- Checks if the worktree directory is present
- Displays an error if the worktree is not found
- Prevents attempting to remove non-existent worktrees
functions/worktree.zsh:86-89
4. Git Worktree Removal
Attempts to remove the worktree using Git:- Executes
git worktree removeon the worktree path - Git validates that the worktree can be safely removed
- Fails if there are uncommitted changes or other issues
functions/worktree.zsh:91-94
5. Post-Removal Hooks
After successful removal:- Calls the
post_wt_rmhook with the removed path - May perform cleanup tasks
- May navigate away from the removed directory
functions/worktree.zsh:96-97
Examples
Remove Feature Branch Worktree
functions/worktree.zsh:96
Using Alias
functions/worktree.zsh:8
Cleanup Workflow
Error Cases
Missing Branch Name
functions/worktree.zsh:68-72
Attempting to Remove Default Branch
functions/worktree.zsh:79-84
Worktree Not Found
repo wt list before attempting removal.
Source: functions/worktree.zsh:86-89
Uncommitted Changes
- Commit your changes:
cd <worktree> && git commit -am "Save work" - Stash your changes:
cd <worktree> && git stash - Force remove (data loss):
cd <worktree> && git worktree remove --force .
functions/worktree.zsh:91-94
Repository Root Not Found
functions/worktree.zsh:75-76
Tips and Best Practices
Check Before Removing
List worktrees to confirm the branch name:Commit or Stash First
Always save your work before removing:Remove After Merging
Clean up feature branches after they’re merged:Batch Cleanup
For removing multiple worktrees, consider:Current Directory Handling
If you’re currently in the worktree being removed:- The
post_wt_rmhook typically navigates you to another worktree - Usually moves you to the default branch worktree
- Prevents issues with working in a removed directory
Default Branch Identification
The default branch is automatically detected:- Usually
mainormaster - Determined by Git’s repository configuration
- Can be checked with:
git symbolic-ref refs/remotes/origin/HEAD
Branch vs Directory Name
The branch name used withrm should match:
- The directory name under the repository root
- The branch name used when creating the worktree
- For branches with slashes (e.g.,
feature/api), use the full name including slashes
Worktree vs Branch
Important distinction:repo wt rmremoves the worktree (working directory)- It does not delete the Git branch itself
- The branch remains and can be used to create a new worktree later
- To delete the branch:
git branch -d <branch-name>
Recovery
If you accidentally remove a worktree:- The Git branch still exists
- Commits are not lost
- Simply recreate:
repo wt add <branch> - Your work is safe as long as it was committed
See Also
- repo wt clean - Remove all non-default worktrees
- repo wt list - View all worktrees
- repo wt add - Create new worktrees
- Worktree Overview - Understanding worktrees