Coding agents like Codex and Claude Code may create Git worktrees while working on tasks. After a while, you can end up with a lot of old worktrees that you no longer need.
To remove all worktrees except your main working tree, run:
1git worktree list | tail -n +2 | awk '{print $1}' | xargs -I {} git worktree remove --force "{}"
This command:
- Lists all Git worktrees.
- Skips the first worktree, which is normally your main working directory.
- Extracts each remaining worktree path.
- Removes each one with
git worktree remove --force.
You can check what will be removed first with:
1git worktree list
Be careful with --force: any uncommitted changes in those worktrees can be discarded.
Once you're done, you can also clean up stale worktree metadata with:
1git worktree prune
Syntax highlighting by Torchlight.dev