Skip to content

Remove All Git Worktrees

Posted by author

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:

  1. Lists all Git worktrees.
  2. Skips the first worktree, which is normally your main working directory.
  3. Extracts each remaining worktree path.
  4. 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

End of article