Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit aba7142

Browse files
fix: clean up empty parent directories after worktree removal (#41)
When using nested branch names like `fix/issue-x`, removing the worktree leaves empty parent directories behind. Add a loop to both wt-rm and wt-destroy that walks up from the target's parent, removing empty directories until it hits the project root or a non-empty directory. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e1fc3c4 commit aba7142

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

template/justfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ wt-rm target:
5858

5959
git worktree remove "$TARGET"
6060

61+
# Remove empty parent directories left after worktree removal
62+
DIR="$(dirname "$TARGET")"
63+
while [ "$DIR" != "." ] && [ -d "$DIR" ] && [ -z "$(ls -A "$DIR")" ]; do
64+
rmdir "$DIR"
65+
DIR="$(dirname "$DIR")"
66+
done
67+
6168
if ! git branch -d "$TARGET" 2>/dev/null; then
6269
echo "Worktree removed but branch '$TARGET' has unmerged changes."
6370
echo " To delete it anyway: git branch -D $TARGET"
@@ -79,6 +86,13 @@ wt-destroy target:
7986

8087
git worktree remove "$TARGET" --force
8188

89+
# Remove empty parent directories left after worktree removal
90+
DIR="$(dirname "$TARGET")"
91+
while [ "$DIR" != "." ] && [ -d "$DIR" ] && [ -z "$(ls -A "$DIR")" ]; do
92+
rmdir "$DIR"
93+
DIR="$(dirname "$DIR")"
94+
done
95+
8296
git branch -D "$TARGET"
8397

8498
if git remote | grep -q . && git ls-remote --exit-code --heads origin "$TARGET" >/dev/null 2>&1; then

0 commit comments

Comments
 (0)