Skip to content
How it worksSafetyPricingQuestions
FR · Français Download
← All guides

Git worktrees left by AI agents: which ones you can remove

Last checked 1 December 2026

A worktree whose pull request or branch is merged, with no unsaved changes, can be removed with git worktree remove and loses nothing. An open pull request or unmerged branch needs a look. One with unsaved changes would lose that work. Claude Code keeps them in .claude/worktrees, Codex in ~/.codex/worktrees.

Why worktrees pile up

A git worktree is a second working copy of a repository, with its own folder and its own checked-out commit, sharing the same history. AI agents use them so that several sessions can edit the same project without stepping on each other. Each session gets a worktree, the work gets merged, and the folder often stays on disk, with a full checkout and sometimes its own node_modules or build output.

Some agents clean up after themselves, but only in some cases:

  • Claude Code removes the worktree of an unnamed session on exit when it is clean, and asks you when it holds work. Worktrees from non-interactive runs (claude -p) are not cleaned up at all.
  • Codex keeps your 15 most recent managed worktrees by default, deletes older ones, and saves a snapshot of their work before it does.

Anything else, including worktrees you kept, worktrees from scripts and worktrees from other tools, stays until you remove it.

Where agents put their worktrees

PathCreated byVerdictWhat you lose
<repo>/.claude/worktrees/<name>Claude Code (--worktree, subagents)Depends on its stateSee the next table
~/.codex/worktreesCodex appDepends on its stateSee the next table
~/orca/workspacesOrcaDepends on its stateSee the next table
<repo>/.worktreesOther tools and scriptsDepends on its stateSee the next table

Codex uses $CODEX_HOME/worktrees, so the folder moves if you set CODEX_HOME or change the worktree root in its settings. To list every worktree of one repository, wherever it lives:

cd ~/path/to/your/repo
git worktree list

Which ones you can remove

What matters is not where a worktree is, but the state of its work.

State of the worktreeExampleVerdictWhat you lose
Its pull request was merged, nothing unsavedfeature-auth, PR mergedSafeNothing: the work is already in your main branch
Its branch is merged, nothing unsavedworktree-bright-running-foxSafeNothing: the work is already in your main branch
Its folder is already gonelisted as prunableSafeNothing: only Git's leftover record of it
Its pull request is still openPR in reviewReviewThe folder; the branch is kept, so the work is not lost
Its branch is not merged, or its PR was closedan abandoned experimentReviewThe folder; the branch and its commits are kept
It has unsaved changeschanged or untracked filesRiskyThat unsaved work, for good
A program is running inside ita dev server, an open sessionNever deleteOuste skips it

Codex creates its worktrees without a branch by default (a detached HEAD). Before removing one where commits were made, check that those commits are on a branch or already merged.

How to check one by hand

From inside the worktree's folder:

git status --short
git branch --show-current
git branch --merged main
gh pr list --head "$(git branch --show-current)" --state all

The first line lists unsaved changes: if it prints anything, stop there. The third line shows whether the branch is already in main (use your default branch's name). The last line needs the GitHub CLI and shows whether the pull request was merged, closed or is still open. Nothing above deletes anything.

How to remove one safely

Use git's own command rather than deleting the folder:

git worktree remove ~/path/to/the/worktree

Git's documentation says git worktree remove only removes clean worktrees, with no untracked files and no changes to tracked files, and refuses the others unless you add --force. That refusal is your safety net: do not add --force unless you know the unsaved work can go.

If you already deleted a worktree folder by hand, git still keeps a record of it. git worktree prune removes the records of worktrees whose folders are missing.

If git says the worktree is locked, an agent may still be using it. Claude Code locks the worktrees of running agents and background sessions so they cannot be removed mid-task.

What you should never delete

Never delete your main checkout, a worktree with unsaved changes you still want, or a worktree a program is running in. And never delete the .git folder of the main repository: every worktree depends on it.

For the rest of what agents leave on your Mac, see where AI coding agents store data on your Mac, what is in ~/.claude and what is in ~/.codex.

Doing it with Ouste

Ouste lists every worktree across your repositories, checks whether its branch is merged, whether its pull request is merged, closed or open when the GitHub CLI is signed in, and whether it holds unsaved changes. It classifies each one as Safe, Review, Risky or Protected with the sentence that says what you lose, and skips any worktree a program is running in. Worktrees are removed with git worktree remove, git's own command, so they do not go through Ouste's 7-day quarantine.

Questions

Where does Claude Code put its worktrees?

In .claude/worktrees/<name> at the root of your repository, on a branch named worktree-<name>.

Is it safe to remove a git worktree?

Yes if it has no unsaved changes and its branch or pull request is merged. git worktree remove refuses worktrees with unsaved changes unless you add --force.

Does removing a worktree delete the branch?

git worktree remove deletes the folder; the branch stays in your repository. Claude Code's own cleanup on exit can remove both, and asks you first when the worktree holds work.

I deleted a worktree folder by hand. What now?

Run git worktree prune in the repository. It removes git's records of worktrees whose folders are missing.

Ouste does this for you. It finds what your agents leave behind, says what each item is and what you lose, and puts anything you might regret in a 7-day quarantine. Free to scan and clean.

Download Ouste

macOS 14 or later · Apple Silicon

Less noise. More control.

Product

  • How it works
  • Safety
  • Pricing
  • Questions

Information

  • Legal notice and terms
  • Privacy
  • Guides
  • contact@ouste.space

Language

  • Français
  • English
© 2026 Ouste. Published by Adeo Consulting, Paris. macOS 14 or later · Apple Silicon