Remove merged worktrees and prune stale branches to keep the repository clean
Use this skill after a feature branch has been merged and the associated worktree is no longer needed. Regular cleanup prevents disk space waste and stale worktree confusion.
| Action | Command |
|---|---|
| List worktrees | git worktree list |
| Remove worktree | git worktree remove worktrees/<name> |
| Force remove | git worktree remove --force worktrees/<name> |
| Prune stale | git worktree prune |
| List merged branches | git branch --merged main |
| Delete local branch | git branch -d <branch> |
| Delete remote branch | git push origin --delete <branch> |
git worktree list
git branch --merged main
# Or check a specific branch (empty output = fully merged):
git log main..feature/142-user-auth --oneline
# Stop any running processes first (dev servers, watchers)
git worktree remove worktrees/142-user-auth
git branch -d feature/142-user-auth
git push origin --delete feature/142-user-auth # If applicable
git worktree remove --force worktrees/142-user-auth
Only use --force when you are certain changes are not needed.
If a worktree directory was deleted manually without git worktree remove:
git worktree prune --dry-run # Preview
git worktree prune # Execute
for wt in $(git worktree list --porcelain | grep 'worktree ' | grep 'worktrees/' | sed 's/worktree //'); do
branch=$(cd "$wt" && git branch --show-current)
if git merge-base --is-ancestor "$branch" main 2>/dev/null; then
echo "Removing merged worktree: $wt (branch: $branch)"
git worktree remove "$wt"
git branch -d "$branch" 2>/dev/null
fi
done
git worktree prune
git fetch --prune
git log main..<branch> --oneline returns emptygit status --short in worktree shows nothinggit worktree list # Confirm cleanup
git branch -a | grep -v main # Check remaining branches
du -sh worktrees/ 2>/dev/null || echo "No worktrees directory"
| Error | Cause | Resolution |
|---|---|---|
worktree is dirty | Uncommitted changes | Commit, stash, or use --force if safe |
branch not fully merged | Using -d on unmerged branch | Use -D only if changes are truly unneeded |
not a worktree | Invalid worktree path | Run git worktree prune to clean stale entries |
directory not empty | Manual files in worktree dir | Remove extra files first |
cannot remove main worktree | Tried to remove primary tree | Only linked worktrees can be removed |
| Remote branch already deleted | Teammate removed it | Run git fetch --prune |
worktree-create skill