How do I clean up git worktrees on my Mac?
A git worktree is a second checkout of the same repository in another folder, on its own branch. AI coding agents create one for every parallel task, and each can hold a full copy of the code plus its own node_modules and build output. This is how to clean up git worktrees without losing work.
The short answer: run git worktree list in the repository, check each worktree for uncommitted changes, then git worktree remove <path> for the ones you're done with and git worktree prune for folders that are already gone. Removing a worktree keeps its branch and commits; what's lost is uncommitted changes and ignored files in that folder, such as .env or a local database.
How do I list and remove a git worktree?
Run these inside the repository, or from anywhere with git -C:
git worktree list
git worktree remove ../my-repo-feature
git -C ~/code/my-repo worktree remove ~/code/my-repo/.claude/worktrees/fix-login
git worktree list shows each worktree's path, its current commit and branch, and marks entries as locked or prunable. git worktree remove deletes the folder, including ignored files such as node_modules, and git's records for it. The main checkout can't be removed this way.
Before removing one, see what it holds:
git -C <path> status --short --ignored
git log --oneline main..<branch>
The first lists uncommitted and untracked files, and with --ignored also ignored ones such as .env, which git worktree remove deletes without warning. The second lists commits on the worktree's branch that aren't in main yet.
Why does git worktree remove refuse to delete it?
- Modified or untracked files: git stops with "contains modified or untracked files, use --force to delete it". That's the check you want. Commit, stash or copy what you need, or add
--forceif you're sure the changes can go. - Locked: git says "cannot remove a locked working tree". Tools lock worktrees they're using. Make sure the agent is finished, then run
git worktree unlock <path>and remove it, or use--forcetwice. - Submodules: a worktree with submodules always needs
--force.
What does git worktree prune do?
If a worktree folder was deleted by hand or moved to the Trash, git still keeps its records in .git/worktrees and git worktree list shows it as prunable. Prune clears those records. Preview it first:
git worktree prune -n -v
git worktree prune
Prune never touches a worktree folder that still exists. Git also prunes stale records by itself during garbage collection, after a delay.
How do I delete the worktree's branch too?
Removing a worktree leaves its branch in the repository. Delete the branch once the worktree is gone:
git branch -d <branch>
-d refuses a branch that isn't fully merged, and git won't delete a branch that's still checked out in another worktree. -D deletes an unmerged branch anyway, along with the only reference to its commits, so push it first if you might need it. A branch merged on GitHub with a squash looks unmerged to git, so check the pull request before using -D.
Where do Claude Code, Codex and Cursor put their worktrees?
- Claude Code:
<repo>/.claude/worktrees/<name>, usually on a branch calledworktree-<name>. When a session ends with no changes, Claude Code removes the worktree and its branch (for a named session it asks first); with changes, it asks whether to keep it. It locks a worktree while an agent works in it. Worktrees fromclaude -pruns aren't removed automatically. - Codex (the app):
~/.codex/worktrees, changeable in its settings. It keeps the 15 most recent worktrees it manages by default and deletes older ones, saving a snapshot first. - Cursor:
~/.cursor/worktrees. It keeps up to 25 per Mac by default (cursor.worktreeMaxCount) and cleans up older ones on a schedule.
Worktrees you kept, or ones from tools that don't clean up, stay until you remove them. If the repository is gone, the worktree folder is just a folder: check it for anything you need and move it to the Trash.
How do I find every worktree on my Mac?
A linked worktree has a .git file, not a folder, that points back to .git/worktrees in its main repository. This finds all of them in your home folder, skipping Library:
find ~ -path ~/Library -prune -o -name .git -type f -exec grep -l '^gitdir: .*/worktrees/' {} + 2>/dev/null | sed 's|/\.git$||'
It reads every folder, so it can take a few minutes, and macOS may ask whether Terminal can access folders like Documents or Desktop. Run du -sh <path> on a result to see its size.
Related: Claude Code, Codex and Cursor disk usage: what's safe to clean, How to find and delete old node_modules folders and package caches on a Mac, Can Claude Code, Cursor or Codex check and clean my Mac's disk?