Safely remove a Git worktree and its directory, handling uncommitted changes and orphaned folders.
Running worktree-remove from inside the main repo:
- Ensures you are running from the main worktree.
- Finds the worktree directory in the parent directory (expects
<repo>-<branch>naming). - Checks if the worktree is registered with Git.
- Safely handles "orphaned" directories (directories that exist but Git no longer recognizes as worktrees).
- Checks for uncommitted changes (if registered) and, when found, asks "Remove anyway?" before proceeding.
- Asks for a final confirmation to remove the registered worktree or orphaned directory.
- Unregisters the worktree from Git (
git worktree remove). - Moves the directory to the system trash instead of permanently deleting it (safer than
rm -rf). - Verifies the removal was successful.
- Node.js ≥ 22.14.0
- Git with
git worktreesupport
You usually don’t need a global install.
# inside /my/path/my-app (main worktree)
# one-off
npx worktree-remove
# or install globally
pnpm add -g worktree-remove # or: npm i -g worktree-remove
worktree-removeRun this inside the main worktree of your project.
If you don't provide a branch name, or use the -i flag, an interactive list of worktrees will be shown:
# inside /my/path/my-app
worktree-remove
# or
worktree-remove -iThis allows you to easily select which worktree to remove from a list.
You can also specify the branch name directly:
worktree-remove <branch>Example:
worktree-remove feature/login-formMIT