Sub topic 12
Removing Worktrees and Branches Safely
This chapter assumes you have created agent worktrees with git worktree add, and that you know how to merge branches and check git status. Once an agent's branch is merged, its worktree and branch are clutter. Cleaning up in the right order keeps you from losing work that has not been merged.
The lifecycle of an agent worktree
A worktree has three parts that need cleanup: the working directory, the branch, and the record Git keeps in the main repository's .git/worktrees folder. Each Git command below handles one or two of these parts.
Step 1: Remove the worktree
After you merge agent/auth, remove its worktree and then its branch:
git worktree remove ../myrepo-auth
git branch -d agent/auth
git worktree pruneEach command has a safety check:
| Command | What it does | Safety check |
|---|---|---|
| `git worktree remove <path>` | Deletes the directory and its record | Refuses if there are uncommitted changes or untracked files |
| `git branch -d <branch>` | Deletes the branch | Refuses if the branch is not merged |
| `git worktree prune` | Clears records of worktrees whose directories no longer exist | None needed, it only removes stale records |
Remove the worktree before the branch. Git will not delete a branch that is still checked out in a worktree.
Step 2: Handle refusals
The refusals are useful. Do not bypass them until you know what they protect.
- "contains modified or untracked files": the agent left changes uncommitted. Run
git -C ../myrepo-auth statusand decide whether to commit them or discard them. The copied.envfile counts as untracked, so you may see this error even when the agent committed everything. Delete the file yourself if you are sure, or use--force. - "not fully merged" from
git branch -d: the branch has commits that are not in your current branch. Review them withgit log main..agent/auth. If you want to throw them away, usegit branch -D agent/auth.
git worktree remove --force ../myrepo-auth
git branch -D agent/auth--force and -D discard work with no undo. A commit on a deleted branch can still be recovered with git reflog for a while, but uncommitted files are gone for good.
Step 3: Prune stale records
If you delete a worktree directory by hand, Git still lists it:
git worktree list
git worktree prune --verboseprune removes the record for each directory that no longer exists. It does not touch branches, so you still need git branch -d afterward.
Protecting worktrees you want to keep
Prune also removes records for worktrees on removable drives or network shares that are temporarily unavailable. Lock a worktree to prevent this and to block accidental removal:
git worktree lock --reason "agent still running" ../myrepo-search
git worktree remove ../myrepo-search # refuses while locked
git worktree unlock ../myrepo-searchA locked worktree also needs git worktree remove -f -f to remove, which is a deliberate double confirmation.
A cleanup checklist
- Confirm the agent process has exited.
- Run
git -C <worktree> statusand commit or discard what is left. - Review the branch with
git diff main...agent/<task>, then merge it. - Run
git worktree remove, thengit branch -d. - Run
git worktree pruneand checkgit worktree list.
Exercise
You run git branch -d agent/billing and Git says the branch is not fully merged. You are sure you want to discard the agent's work. What command do you run, and what should you check first?
Show answer
Run git branch -D agent/billing. First check what you are discarding with git log main..agent/billing, and make sure the worktree for that branch was already removed (or remove it with git worktree remove --force), because a branch checked out in a worktree cannot be deleted.
Exercise
You deleted ../myrepo-search with a file manager. git worktree list still shows it. How do you fix this, and does it also delete the agent/search branch?
Show answer
Run git worktree prune. It clears the stale record but does not delete the branch, so run git branch -d agent/search (or -D if unmerged) separately.