Skip to content

Feature: Delete associated worktrees when removing stale branches #143

@Sixeight

Description

@Sixeight

Description

Summary

Add worktree support to gh-poi: when a branch is identified as deletable, automatically remove any associated worktree before deleting the branch.

Motivation

Git worktrees allow developers to have multiple working directories from a single repository. A common workflow is:

git worktree add ../feature-123 feature-123
# work on feature, create PR, merge
gh poi  # currently fails or leaves orphaned worktree

Currently, gh poi cannot delete branches that have associated worktrees because:

  1. Git refuses to delete a branch checked out in any worktree
  2. After manual branch deletion, orphaned worktrees remain on disk

This forces users to manually manage worktrees, defeating the purpose of automated cleanup.

Proposed Solution

Behavior

  1. Detection: Before attempting branch deletion, check if the branch has an associated worktree using git worktree list
  2. Removal: For deletable branches with worktrees, run git worktree remove <path> before git branch -D
  3. Display: Show worktree paths in the output when they will be removed

New Flag

Flag Description
--worktree Also remove worktrees associated with deletable branches

Output Example

$ gh poi --worktree

Deleting branches...
  ✓ feature-123 (worktree: /home/user/projects/feature-123)
  ✓ bugfix-456 (worktree: /home/user/projects/bugfix-456)
  ✓ hotfix-789

3 branches deleted (2 worktrees removed)

Dry-run Support

$ gh poi --dry-run --worktree

Branches to delete:
  - feature-123 (worktree: /home/user/projects/feature-123)
  - bugfix-456 (worktree: /home/user/projects/bugfix-456)
  - hotfix-789

Additional context

Edge Cases

Case Handling
Locked worktree Skip with warning (user must run git worktree unlock manually)
Worktree has uncommitted changes Skip with warning (user must handle manually)
Main worktree (repo root) Never remove (implicit protection)
Worktree path no longer exists Run git worktree prune first

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions