git worktree remove: clean up agent worktrees without losing uncommitted work
TL;DR: One worktree per agent task is the safest layout, and it leaves you with a cleanup problem. git worktree remove <path> is cautious about tracked changes and untracked files: it refuses with fatal: '<path>' contains modified or untracked files, use --force to delete it. It is not cautious about ignored files. In the reproduction I ran today on Git 2.47.3, a worktree whose only content was a gitignored node_modules/ and a gitignored .env was removed with exit code 0 and no prompt. And --force on a worktree with an untracked file deletes that file for good: the object was never in Git, so git fsck --lost-found printed nothing. The safe order is git status, then git stash push -u or a commit, then git worktree remove, then git worktree prune for whatever the agent deleted behind Git’s back.
What you’ll be able to do:
- Predict which worktrees
git worktree removewill delete silently, which it will refuse, and which it will keep. - Save an agent’s half-finished work from inside a worktree before you remove it, and find it again from the main checkout.
- Clear the
prunableentries that pile up when an agent or a script runsrm -rfon a worktree directory. - Protect a worktree that is still running a task with
git worktree lock.
Honest take: every command and every line of output below was reproduced on November 21, 2026 on Git 2.47.3 on Linux, in a throwaway repository under
/tmp. I did not test other Git versions, other operating systems, or any specific coding agent. The messages come from Git, not from an agent, so they apply to whichever tool created the worktree. The SHAs are from that reproduction and will not match yours.
Why agent workflows end up with a lot of worktrees
The pattern in the git worktrees for AI coding guide is one worktree and one branch per task: the agent works in ../wt-task1 on agent/task-1, and a git branch -D or a bad rebase in one task cannot touch another. That is the right default, and it is also why a busy week leaves you with a dozen sibling directories.
Git records each linked worktree as a small directory under .git/worktrees/. The working files live wherever you put them. Two things can go out of sync: the directory can disappear without Git being told, and the directory can still hold work that Git has never seen. The two halves of this article are those two problems.
The setup for the reproduction is one commit on main, then two worktrees created the way an agent harness would create them:
$ git worktree add -b agent/task-1 ../wt-task1
Preparing worktree (new branch 'agent/task-1')
HEAD is now at 76b91bb base
$ git worktree add -b agent/task-2 ../wt-task2
Preparing worktree (new branch 'agent/task-2')
HEAD is now at 76b91bb base
Then I left each one dirty in a different way: an untracked draft.txt in wt-task1, and an appended line in the tracked app.txt in wt-task2.
What git worktree remove refuses, and what it does not
The Git manual for git worktree says: “Only clean worktrees (no untracked files and no modification in tracked files) can be removed. Unclean worktrees or ones with submodules can be removed with —force.” Both dirty worktrees behave exactly as that sentence says:
$ git worktree remove ../wt-task1
fatal: '../wt-task1' contains modified or untracked files, use --force to delete it
[exit 128]
$ git worktree remove ../wt-task2
fatal: '../wt-task2' contains modified or untracked files, use --force to delete it
[exit 128]
Exit code 128 is a hard failure, so a cleanup script written with set -e stops there. That is the behavior you want. The next result is the one that surprises people.
I made a clean worktree, added node_modules/ to .gitignore, and put a file inside node_modules/ in the new worktree. Then I removed it with no flags:
$ git worktree remove ../wt-task5
[exit 0]
The directory was gone. I repeated it with .env in .gitignore and a .env file containing SECRET=1 in the worktree. Same result: exit 0, directory gone, no warning. For Git, an ignored file does not make a worktree “unclean”. That is reasonable for node_modules and a build cache, which you can regenerate. It is a trap for the gitignored files you cannot regenerate: a local .env, a sqlite database, a downloaded model file, an agent’s scratch notes in a .gitignored notes/ folder.
| Worktree contents | git worktree remove | Result |
|---|---|---|
| Clean | accepted | removed |
Only gitignored files (node_modules/, .env) | accepted | removed, ignored files deleted without a prompt |
Untracked file (draft.txt) | refused, exit 128 | nothing deleted |
| Modified tracked file | refused, exit 128 | nothing deleted |
| Locked | refused | see the lock section below |
If you let agents keep a .env or a data file inside a worktree, copy it out first or keep it outside the worktree and symlink it in.
What —force does to your work
--force is the documented way to remove an unclean worktree, and it does what it says. After git worktree remove --force ../wt-task1, git status in the main checkout was clean and the worktree listing no longer included wt-task1. The untracked draft.txt went with the directory.
A tracked modification is the same story: the edit existed only as a change in the working directory. An untracked file is worse, because there is no object to find. To check, I created a fresh worktree, put a file called notes.txt containing precious in it, and removed it with --force:
$ git worktree remove --force ../wt-task7
[exit 0]
$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
That progress line is what a terminal shows. Redirect it into a file and the same command prints nothing at all, which is worth knowing before you wire it into a script that greps for output.
git fsck --lost-found found nothing to recover. This is the opposite of the case in the git reflog recovery article, where a deleted branch left its commits behind as unreachable objects. A file that was never staged and never committed has no commit, no blob, and nothing for the reflog to point at. --force is a one-way door for exactly the files you most want to keep.
One thing git worktree remove does not do is delete the branch. After the removals above, git branch still listed agent/task-1 and agent/task-2. Any commits the agent made on those branches are safe and reachable, and you decide separately whether to delete the branch with git branch -d (which refuses if the branch is not merged) or git branch -D (which does not refuse).
The safe sequence before you remove a worktree
Three commands, in this order. The first one is the check, and it costs one line in an agent runbook:
$ git -C ../wt-task6 status --short
M app.txt
?? draft.txt
Then save the work. For a draft you do not want to commit yet, git stash push -u stashes tracked changes and untracked files together. I ran it from inside the worktree:
$ git -C ../wt-task6 stash push -u -m "agent task-6 wip"
Saved working directory and index state On agent/task-6: agent task-6 wip
Now the plain remove, with no --force, succeeds because the worktree is clean:
$ git worktree remove ../wt-task6
[exit 0]
The point of doing the stash from a worktree is that the stash lives in the repository, not in the worktree directory. The Git manual describes a linked worktree as sharing “everything except per-worktree files such as HEAD, index, etc.”, and the stash list is part of what is shared. From the main checkout, after the worktree was already gone:
$ git stash list
stash@{0}: On agent/task-6: agent task-6 wip
$ git stash show -p --include-untracked stash@{0}
diff --git a/app.txt b/app.txt
index 7898192..d49c2e7 100644
--- a/app.txt
+++ b/app.txt
@@ -1 +1,2 @@
a
+more
diff --git a/draft.txt b/draft.txt
new file mode 100644
index 0000000..e712304
--- /dev/null
+++ b/draft.txt
@@ -0,0 +1 @@
+wip
Both the tracked edit and the untracked draft.txt are in the stash. git stash pop in any checkout brings them back. If the work is more than a scratch draft, commit it on the task branch instead. The branch survives the worktree either way.
Note what stash push -u does not take: ignored files. The .env case above is unchanged by a stash. Check git status --ignored --short if you suspect the agent wrote anything you care about into an ignored path.
When the directory is already gone: prunable worktrees
The other failure mode is the agent, or a script, or you in a hurry, running rm -rf ../wt-task2 instead of git worktree remove. Git is not told. The metadata under .git/worktrees/ stays, and so does the branch checkout lock. git worktree list now flags the entry:
$ git worktree list
/tmp/wtx/repo 2ed7840 [main]
/tmp/wtx/wt-task2 76b91bb [agent/task-2] prunable
The porcelain output, which is the format to use in a script, spells out the reason:
$ git worktree list --porcelain
worktree /tmp/wtx/repo
HEAD 2ed7840c68f77caa293a88acba926e225e43d191
branch refs/heads/main
worktree /tmp/wtx/wt-task2
HEAD 76b91bba2fd5b4455b390f77f01322e24568e7ef
branch refs/heads/agent/task-2
prunable gitdir file points to non-existent location
git worktree prune removes those administrative entries. Run it with --dry-run -v first; it prints what it would do and changes nothing:
$ git worktree prune --dry-run -v
Removing worktrees/wt-task2: gitdir file points to non-existent location
$ git worktree prune -v
Removing worktrees/wt-task2: gitdir file points to non-existent location
$ git worktree list
/tmp/wtx/repo 2ed7840 [main]
Like remove, prune leaves the branch alone: agent/task-2 was still in git branch afterwards. You do not have to run it by hand forever, because git gc calls git worktree prune --expire 3.months.ago; the gc.worktreePruneExpire setting in the Git manual changes the grace period. Three months is a long time to leave a stale entry that blocks you from reusing the same branch name in a new worktree, so a manual prune at the end of an agent session is cheap insurance.
Until you prune, the stale entry still owns its branch. I deleted a worktree directory by hand and then tried both obvious next steps:
$ git branch -D agent/task-9
error: cannot delete branch 'agent/task-9' used by worktree at '/tmp/wtx/wt-task9'
$ git worktree add ../wt-again agent/task-9
fatal: 'agent/task-9' is already used by worktree at '/tmp/wtx/wt-task9'
Both fail until git worktree prune has run. The same message appears for a branch that is checked out in a live worktree, which is the guard you will hit when you try to clean up a branch a task still holds:
$ git branch -D agent/task-4
error: cannot delete branch 'agent/task-4' used by worktree at '/tmp/wtx/wt-task4'
That message is useful. If you see it for a branch you thought was finished, a worktree for it is still registered, and git worktree list will tell you where.
Locking a worktree an agent is still using
git worktree lock exists for worktrees on removable drives or network shares, but it is also a good fit for a task that must not be cleaned up. The manual says a locked worktree is protected from automatic pruning, moving and deletion. I locked one with a reason, deleted its directory by hand, and ran prune:
$ git worktree lock --reason "agent running" ../wt-task3
$ git worktree prune -v
$ git worktree list
/tmp/wtx/repo 2ed7840 [main]
/tmp/wtx/wt-task3 2ed7840 [agent/task-3] locked
prune -v printed nothing and the entry stayed, labelled locked, even though its directory was gone. Removal is refused too, and the reason you gave is in the message:
$ git worktree remove ../wt-task3
fatal: cannot remove a locked working tree, lock reason: agent running
use 'remove -f -f' to override or unlock first
Unlock first, and then the normal cleanup works:
$ git worktree unlock ../wt-task3
$ git worktree prune -v
Removing worktrees/wt-task3: gitdir file points to non-existent location
A harness that creates a worktree could lock it with a reason naming the task, and unlock it when the task ends. Then a cleanup job that runs git worktree prune or a tidy-minded teammate cannot take a live task’s checkout out from under it.
A cleanup script that fails safe
Put the pieces together as a conservative shell loop. It lists every linked worktree, skips locked ones, refuses to touch anything with changes, and only then removes:
git worktree prune -v
git worktree list --porcelain | awk '/^worktree /{print $2}' | tail -n +2 | while read -r wt; do
if [ -n "$(git -C "$wt" status --short --ignored 2>/dev/null)" ]; then
echo "SKIP (has changes or ignored files): $wt"
continue
fi
git worktree remove "$wt"
done
The tail -n +2 skips the first entry, which is always the main worktree. --ignored in the check is deliberate: it makes the loop stop at a worktree that holds only gitignored files, which plain git worktree remove would delete. A locked worktree is refused by remove without -f -f, so the loop never forces it. I ran this exact loop against three throwaway worktrees: a clean one, one with an untracked u.txt, and a locked one. The clean one was removed, the dirty one printed the SKIP line and survived, and the locked one printed fatal: cannot remove a locked working tree, lock reason: agent running followed by use 'remove -f -f' to override or unlock first, with the loop moving on. Run it against a throwaway repository before you point it at anything you care about, because it has no dry-run mode.
Related guides on this site cover the other places agent work disappears: the reflog runbook for deleted branches and reset --hard, git rerere for repeated conflicts, and checking a patch before you retry it for patches that half-apply.
Honest take
git worktree remove is safe for tracked changes and untracked files, and unsafe for ignored files. --force is unsafe for anything that is not committed. If an agent harness removes worktrees automatically, the two defaults to change are: never pass --force, and run a status --ignored check first. Treat prune as housekeeping that follows a removal, not as part of it.
None of this tells you that your agent is doing something wrong. Most agents and harnesses create and delete worktrees correctly. The risk is the gap between “Git thinks this worktree is disposable” and “I have something in there that is not in Git”, and that gap is one status command wide.
Sources
- git-worktree(1) —
add,remove(only clean worktrees;--force),prune,lock,unlock,list --porcelain— git-scm.com - git-config(5) —
gc.worktreePruneExpire(git gccallsgit worktree prune --expire 3.months.ago) — git-scm.com - git-stash(1) —
git stash push --include-untrackedandgit stash show— git-scm.com - git-fsck(1) —
--lost-found— git-scm.com - git-branch(1) —
-dversus-D, and the refusal for a branch checked out in a worktree — git-scm.com - gitignore(5) — what Git treats as an ignored file — git-scm.com
- Reproduction output: Git 2.47.3 on Linux, November 21, 2026 —
git worktree add -b,git worktree removewith and without--force,git worktree list --porcelain,git worktree prune --dry-run -v,git worktree lock --reasonandunlock,git stash push -u,git stash show -p --include-untracked,git fsck --lost-found,git branch -Don a checked-out branch
Last updated November 21, 2026. Every command output in this article was reproduced on November 21, 2026 with Git 2.47.3 on Linux in a throwaway repository. The SHAs and paths are from that reproduction and will not match your repository. Verify the state of each worktree with git status --short --ignored before you remove it, because git worktree remove --force cannot be undone.
Was this article helpful?
Thanks for the feedback — it helps improve future articles.
Need hands-on help?
I offer 1-on-1 technical consulting for local AI setup, GPU selection, and AI coding tool configuration — same topics covered on this site.
Book a session — $49 / hour →Know which coding tool is worth paying for
Hands-on comparisons of AI coding assistants and what each one costs to run — including the local-model path. Sent only when something changes. Unsubscribe anytime.