Sub topic 7
Removing worktrees and pruning stale metadata
This chapter assumes you can create a linked worktree with git worktree add, and that you know the difference between the working directory, the index and the repository history. You should also be comfortable with git worktree list.
What a worktree leaves behind
A linked worktree has two parts. One is the directory you created, such as ../myproject-hotfix. The other is a small administrative entry inside the shared repository, under .git/worktrees/. That entry records the worktree's path, its HEAD and its index.
Git needs both parts to agree. If you remove only one of them, the other is left orphaned. Cleanup is the work of keeping them in sync.
Removing a worktree with git worktree remove
Once a task is finished and merged, remove its worktree with one command:
git worktree remove ../myproject-hotfix
git worktree prunegit worktree remove deletes the directory and the matching administrative entry together. It does not delete the branch. The branch and its commits stay in the repository until you delete the branch yourself.
Git refuses to remove a worktree in two cases:
- The worktree has modified or untracked files. Git protects work that exists nowhere else.
- The worktree is locked. A lock means someone asked Git to keep it.
To discard the changes on purpose, add --force. A locked worktree needs --force given twice, or you can unlock it first.
git worktree remove --force ../myproject-hotfixUse --force only after you have checked what would be lost. Git cannot recover uncommitted files once the directory is gone.
You cannot remove the main worktree this way. The directory that holds the original .git directory is the repository itself.
Pruning stale metadata with git worktree prune
Sometimes a worktree directory disappears without Git knowing. You might run rm -rf ../myproject-hotfix, or a cleanup tool might delete it. The administrative entry remains, and git worktree list shows the worktree marked prunable.
git worktree prune deletes the entries whose directories no longer exist:
git worktree list
git worktree prune --verboseThe --verbose option prints each entry it removes. Add --dry-run to see what would be pruned without changing anything.
Git also prunes automatically. Routine garbage collection (git gc) removes stale entries after the gc.worktreePruneExpire period, which defaults to three months. Do not rely on this. Run prune yourself when you notice a stale entry.
<br>
The branch is still there
Removing a worktree frees the branch for use elsewhere. This also resolves the "already checked out" error from earlier. After removal, you can check the same branch out in another worktree.
To finish the job, delete the branch if it is merged:
git branch -d hotfix/login-crash-d refuses to delete a branch that has unmerged commits. Use -D only when you are sure you want to discard them.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Deleting the directory with `rm -rf` | Stale entry stays and the branch remains marked as checked out | Run `git worktree prune` |
| Using `--force` without looking | Uncommitted changes are lost | Run `git status` inside the worktree first |
| Expecting `remove` to delete the branch | The branch survives | Run `git branch -d` |
| Removing the worktree you are standing in | The shell is left in a deleted directory | Change to another directory first |
The second row matters most. Before forcing a removal, check what you would lose:
git -C ../myproject-hotfix status --shortIf the output is empty, the worktree is clean and a plain remove will work.
Exercise
You deleted a worktree directory with rm -rf ../myproject-review. Later, git worktree add ../myproject-review2 feature/search fails because feature/search is "already checked out". What is the cause, and what command fixes it?
Show answer
The administrative entry for the deleted worktree still exists, so Git believes feature/search is checked out at the old path. Run git worktree prune (optionally with --dry-run first) to remove the stale entry. Then repeat the git worktree add command. If the old worktree had been locked, unlock it first, because locked entries are not pruned.
Exercise
git worktree remove ../myproject-feature fails because the worktree contains untracked files. How do you check what would be lost, and what do you do if the files are disposable?
Show answer
Run git -C ../myproject-feature status --short to list the untracked and modified files. If they are disposable, run git worktree remove --force ../myproject-feature. If any file matters, copy it out or commit it first.