Sub topic 7
Removing Worktrees and Pruning Stale Metadata
This chapter assumes you can create a linked worktree with git worktree add and read the output of git worktree list. It also assumes you know that a branch can be checked out in only one worktree at a time. You should be comfortable with git status, git branch -d and git branch -D, and with the difference between tracked, untracked and ignored files.
A worktree is cheap to create, so they tend to pile up. Each one holds a directory on disk and a small record inside the repository. This chapter covers how to retire a worktree safely, what Git checks before it deletes anything, and how to clean up when a worktree directory has already been deleted by other means.
What a worktree consists of
Cleanup touches two separate places, which is why there are two commands.
- The working directory. This is the folder you created, for example
../myproject-hotfix. It contains your checked-out files and a small file named.git. That file is not a directory. It holds one line, such asgitdir: /home/you/myproject/.git/worktrees/myproject-hotfix. - The administrative directory. This lives inside the main repository at
.git/worktrees/<name>. It holds the per-worktreeHEAD, the index, and agitdirfile that points back to the working directory.
The two sides reference each other. git worktree remove deletes both sides in one step. git worktree prune deletes only administrative directories whose working directory no longer exists.
Removing a worktree with git worktree remove
The normal way to retire a worktree is:
git worktree remove ../myproject-hotfixRun this from the main repository or from any other worktree. Do not run it from inside the worktree you are removing. Git deletes the directory and the matching entry under .git/worktrees.
Before deleting, Git checks that the worktree is clean. It refuses if there are modified tracked files or untracked files. This protects work you have not committed.
fatal: '../myproject-hotfix' contains modified or untracked files, use --force to delete itGit does not count ignored files as untracked. A node_modules directory or a build output folder listed in .gitignore will not block removal, and it is deleted along with the rest of the directory. If you keep something valuable in an ignored file, such as a local .env, copy it out first.
Forcing removal
--force (or -f) removes a dirty worktree and discards the uncommitted changes in it.
git worktree remove --force ../myproject-hotfixUse it only after you have looked at what would be lost. Two other cases also need --force:
- A worktree that contains initialized submodules.
- A locked worktree. Git requires the flag twice for this case:
git worktree remove -f -f <path>. The double flag is deliberate. A lock means someone asked Git to protect that worktree, and the second flag confirms you are overriding it. Locking is covered in a later chapter.
You cannot remove the main worktree, the one that contains the real .git directory. To get rid of it you delete the repository itself.
What remove does not delete
git worktree remove deletes the directory and the administrative record. It does not delete the branch. After removing ../myproject-hotfix, the branch hotfix/login-crash still exists and keeps all its commits. This is usually what you want, because the commits should stay until they are merged. The branch is free to be checked out again, in this worktree or a new one.
When the work is merged, delete the branch as a separate step:
git worktree remove ../myproject-hotfix
git branch -d hotfix/login-crashgit branch -d refuses if the branch has commits that are not merged into your current branch or its upstream. That refusal is a useful safety check. Use -D only when you are sure you want to discard the commits.
Remote branches are also separate. If you pushed the branch, delete the remote copy with git push origin --delete hotfix/login-crash when it is no longer needed.
Checking before you remove
A short routine catches most mistakes. Replace the path with your own.
git worktree list
git -C ../myproject-hotfix status --short
git -C ../myproject-hotfix log --oneline @{upstream}..HEADThe first command confirms the path and branch. The second shows uncommitted changes. The third shows commits on the worktree's branch that are not on its upstream branch. It only works if the branch has an upstream configured. If it does not, compare against another branch, for example git log --oneline main..HEAD.
The detached HEAD trap
A worktree created with --detach has no branch. Suppose you made commits there and never created a branch for them. When you remove the worktree, Git deletes its administrative directory, and that directory contains the reflog for that HEAD. The commits are now unreachable. They still exist in the object database until garbage collection, and git fsck --lost-found can list them, but no normal command will point to them.
Before removing a detached worktree that has commits you want, give them a name:
git -C ../myproject-review switch -c review/notesYou can then remove the worktree and keep the branch. If you only read code in the worktree, there is nothing to save.
Stale metadata and git worktree prune
Sometimes the working directory disappears without Git being told. Common causes are:
- Running
rm -rf ../myproject-hotfixinstead ofgit worktree remove. - A file manager or cleanup tool deleting old folders.
- A temporary directory being wiped on reboot.
- Moving the folder by hand, which breaks the link just as deletion does. The repair command for that case is covered in a later chapter.
The administrative directory under .git/worktrees is still there, so Git still believes that worktree exists. This has two visible effects. The worktree appears in git worktree list, and its branch still counts as checked out. A git switch hotfix/login-crash in another worktree can fail with an "already checked out" error even though the directory is gone.
In Git 2.31 and newer, git worktree list marks such entries as prunable:
/home/you/myproject 3f2a1c9 [main]
/home/you/myproject-hotfix 8be41d0 [hotfix/login-crash] prunableUse git worktree list --verbose to see the reason on the following line, such as gitdir file points to non-existent location.
Running prune
git worktree prune deletes the administrative entries for worktrees whose directories are gone:
git worktree prune --dry-run --verbose
git worktree prune --verbose--dry-run (-n) reports what would be removed and changes nothing. --verbose (-v) prints each entry it removes. Running the dry run first is a good habit, because of the reason a directory can be missing that is not an accident: a worktree on a removable drive or network share that is simply not mounted right now. Pruning would forget it.
Prune skips locked worktrees. If a worktree lives on a drive that is sometimes unplugged, lock it, and prune will leave it alone. Locking is covered in a later chapter.
Automatic pruning
You usually do not need to run prune on a schedule. git gc prunes stale worktree entries that are older than the setting gc.worktreePruneExpire, which defaults to three months. Git runs gc automatically now and then, so entries for deleted directories eventually disappear. Until that happens, the stale entry can still block branch checkouts. Run git worktree prune yourself when you notice the problem.
You can also pass --expire <time> to prune only entries older than a given age:
git worktree prune --expire 2.weeks.agoA complete runnable walk-through
This script builds a scratch repository, so you can watch each case without touching a real project. It needs Git 2.28 or newer for git init -b.
set -e
cd "$(mktemp -d)"
git init -q -b main demo
cd demo
git -c user.name=demo -c [email protected] commit -q --allow-empty -m "initial"
# Case 1: a clean removal, followed by branch cleanup
git worktree add -q ../demo-fix -b fix/one
git worktree remove ../demo-fix
git branch -d fix/one
# Case 2: Git refuses to remove a dirty worktree
git worktree add -q ../demo-dirty -b fix/two
echo "scratch" > ../demo-dirty/notes.txt
git worktree remove ../demo-dirty || echo "refused: worktree is not clean"
git worktree remove --force ../demo-dirty
git branch -d fix/two
# Case 3: a directory deleted by hand leaves stale metadata
git worktree add -q ../demo-gone -b fix/three
rm -rf ../demo-gone
git worktree list
git worktree prune --dry-run --verbose
git worktree prune --verbose
git worktree list
git branch -d fix/threeIn case 3, the first git worktree list still shows demo-gone. After prune, only the main worktree remains. The final git branch -d fix/three succeeds because the branch points at the same commit as main, so Git treats it as merged.
Choosing the right command
| Situation | Command | Notes |
|---|---|---|
| Worktree is finished and clean | `git worktree remove <path>` | Deletes the directory and the record. The branch stays. |
| Worktree has changes you want to discard | `git worktree remove --force <path>` | Uncommitted work is lost. |
| Worktree is locked | `git worktree remove -f -f <path>` | Consider unlocking first. |
| Folder was already deleted by hand | `git worktree prune` | Check with `--dry-run` first. |
| Branch is merged and no longer needed | `git branch -d <branch>` | Separate from worktree removal. |
| Detached worktree with commits to keep | `git switch -c <name>` inside it, then remove | Otherwise the commits become unreachable. |
Common errors
- "contains modified or untracked files". The worktree is dirty. Commit, stash or discard the changes, or use
--forceonce you have checked what would be lost. - "is a main working tree". You tried to remove the primary checkout. Linked worktrees can be removed. The main one cannot.
- "cannot remove a locked working tree". Someone locked it. Run
git worktree list --verboseto read the lock reason before overriding it. - "already checked out at ..." for a path that no longer exists. The metadata is stale. Run
git worktree prune. - Branch deletion refused with "not fully merged". The branch has commits that exist nowhere else. Merge it, push it, or confirm you want to discard it with
-D.
Exercise
You run rm -rf ../myproject-hotfix to clean up, then git switch hotfix/login-crash in the main repository fails with an "already checked out" error pointing at the deleted path. Why does it fail, and which commands fix it without losing any commits?
Show answer
The folder is gone, but its administrative directory under .git/worktrees still records that hotfix/login-crash is checked out there. Run git worktree prune (optionally with --dry-run --verbose first) to delete the stale entry. The branch and its commits were never deleted, so git switch hotfix/login-crash then works.
Exercise
You created a worktree with git worktree add --detach ../review origin/feature/search, made two commits inside it, and now want to remove the worktree. What should you do before running git worktree remove ../review, and why?
Show answer
Create a branch at the current commit, for example git -C ../review switch -c review/notes. A detached HEAD has no branch, and removing the worktree deletes the administrative directory that holds its reflog. Without a branch, the two commits become unreachable and can only be found with git fsck --lost-found until garbage collection removes them.
Exercise
A worktree contains an ignored node_modules directory and no other changes. Will a plain git worktree remove succeed, and what happens to node_modules?
Show answer
Yes, it succeeds, because Git does not count ignored files as untracked. node_modules is deleted together with the rest of the worktree directory.