You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 18, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 18, 2026. It is now read-only.
The codebase already implements merged branch cleanup functionality in cmd_clean --merged (file:bin/gtr lines 926-1024), which uses GitHub CLI to detect merged PRs and removes associated worktrees. However, this feature is undocumented in the README and the user is requesting it under the rm command instead. The current implementation checks for uncommitted changes, untracked files, and uses gh pr list to verify merge status before removal.
Approach
Add a --merged flag to the existing cmd_remove function to enable bulk removal of worktrees with merged branches. This provides a more intuitive UX (git gtr rm --merged) compared to the existing clean --merged command. The implementation will reuse the merge detection logic from cmd_clean but integrate it into the rm command's workflow, maintaining consistency with existing flags like --delete-branch, --force, and --yes.
Implementation Steps
1. Extend cmd_remove Function Flag Parsing
Location: file:bin/gtr lines 338-368
Add --merged flag handling in the argument parser:
Add a merged_mode variable (default: 0)
Parse --merged flag to set merged_mode=1
When --merged is set, ignore the identifiers requirement check (line 370-373)
Add --dry-run flag support for preview mode
2. Extract Merge Detection Logic into Reusable Function
Location: file:lib/core.sh (new function at end)
Create a new function is_worktree_merged() that encapsulates the merge detection logic:
Add example: git gtr rm --merged --yes for automated cleanup
Add note about GitHub CLI requirement for --merged flag
6. Update CHANGELOG
Location: file:CHANGELOG.md lines 7-10
Add to the [Unreleased] section under ### Added:
"--merged flag for rm command to bulk remove worktrees with merged PRs (requires GitHub CLI)"
"--dry-run flag for rm command to preview removals"
Sequence Diagram
sequenceDiagram
participant User
participant cmd_remove
participant is_worktree_merged
participant gh CLI
participant remove_worktree
User->>cmd_remove: git gtr rm --merged
cmd_remove->>cmd_remove: Parse --merged flag
cmd_remove->>gh CLI: Verify gh CLI available
cmd_remove->>cmd_remove: git fetch origin --prune
loop For each worktree
cmd_remove->>is_worktree_merged: Check if merged
is_worktree_merged->>is_worktree_merged: Check uncommitted changes
is_worktree_merged->>is_worktree_merged: Check untracked files
is_worktree_merged->>gh CLI: gh pr list --head branch --state merged
gh CLI-->>is_worktree_merged: PR state
is_worktree_merged-->>cmd_remove: merged status
alt If merged and clean
cmd_remove->>remove_worktree: Remove worktree
cmd_remove->>cmd_remove: Delete branch
else If not merged or dirty
cmd_remove->>cmd_remove: Skip with warning
end
end
cmd_remove-->>User: Summary (removed/skipped counts)
Loading
Additional Considerations
Backward Compatibility: The existing clean --merged functionality should remain unchanged to avoid breaking existing workflows
Error Handling: Ensure proper error messages when gh CLI is not installed or not authenticated
Safety Checks: Maintain existing safety checks (uncommitted changes, untracked files) to prevent data loss
Dry Run Mode: The --dry-run flag allows users to preview what would be removed before committing to the operation
Confirmation Prompts: Respect the --yes flag for non-interactive mode, otherwise prompt for each worktree removal
Import In IDE
🤖 Prompt for AI Agents
## Observations
The codebase already implements merged branch cleanup functionality in `cmd_clean --merged` (file:bin/gtr lines 926-1024), which uses GitHub CLI to detect merged PRs and removes associated worktrees. However, this feature is undocumented in the README and the user is requesting it under the `rm` command instead. The current implementation checks for uncommitted changes, untracked files, and uses `gh pr list` to verify merge status before removal.
## Approach
Add a `--merged` flag to the existing `cmd_remove` function to enable bulk removal of worktrees with merged branches. This provides a more intuitive UX (`git gtr rm --merged`) compared to the existing `clean --merged` command. The implementation will reuse the merge detection logic from `cmd_clean` but integrate it into the `rm` command's workflow, maintaining consistency with existing flags like `--delete-branch`, `--force`, and `--yes`.
## Implementation Steps### 1. Extend `cmd_remove` Function Flag Parsing**Location:** file:bin/gtr lines 338-368
Add `--merged` flag handling in the argument parser:
- Add a `merged_mode` variable (default: 0)
- Parse `--merged` flag to set `merged_mode=1`- When `--merged` is set, ignore the `identifiers` requirement check (line 370-373)
- Add `--dry-run` flag support for preview mode
### 2. Extract Merge Detection Logic into Reusable Function**Location:** file:lib/core.sh (new function at end)
Create a new function `is_worktree_merged()` that encapsulates the merge detection logic:
- Parameters: `worktree_path`, `branch_name`, `main_branch`- Check for uncommitted changes using `git diff --quiet` and `git diff --cached --quiet`- Check for untracked files using `git ls-files --others --exclude-standard`- Query GitHub PR status using `gh pr list --head "$branch" --state merged --json state --jq '.[0].state'`- Return 0 if merged and clean, 1 otherwise
- Include appropriate logging for skip reasons (uncommitted changes, untracked files, not merged)
### 3. Implement Merged Mode in `cmd_remove`**Location:** file:bin/gtr lines 380-432
Add conditional logic after line 378 (after resolving repo context):
- If `merged_mode=1`, perform bulk merged branch removal:
- Verify `gh` CLI is available (similar to `cmd_clean` lines 930-941)
- Fetch from origin with `git fetch origin --prune`- Get main branch using `current_branch "$repo_root"`- Iterate through worktree directories in `$base_dir/${prefix}*`- For each worktree, call `is_worktree_merged()` function
- If merged, proceed with removal using existing removal logic (lines 394-432)
- Track counters for removed/skipped worktrees
- Display summary at the end
- If `merged_mode=0`, use existing identifier-based removal logic
### 4. Update Help Text**Location:** file:bin/gtr lines 1431-1435
Extend the `rm` command documentation:
- Add `--merged` flag description: "remove all worktrees with merged PRs (requires gh CLI)"
- Add `--dry-run, -n` flag description: "preview what would be removed"
- Add usage example: `git gtr rm --merged --yes` for non-interactive cleanup
### 5. Update README Documentation**Location:** file:README.md lines 216-226
Enhance the `git gtr rm` section:
- Add `--merged` to the options list with description
- Add example: `git gtr rm --merged --dry-run` to preview merged branches
- Add example: `git gtr rm --merged --yes` for automated cleanup
- Add note about GitHub CLI requirement for `--merged` flag
### 6. Update CHANGELOG**Location:** file:CHANGELOG.md lines 7-10
Add to the `[Unreleased]` section under `### Added`:
- "`--merged` flag for `rm` command to bulk remove worktrees with merged PRs (requires GitHub CLI)"
- "`--dry-run` flag for `rm` command to preview removals"
## Sequence Diagram```mermaidsequenceDiagram participant User participant cmd_remove participant is_worktree_merged participant gh CLI participant remove_worktree User->>cmd_remove: git gtr rm --merged cmd_remove->>cmd_remove: Parse --merged flag cmd_remove->>gh CLI: Verify gh CLI available cmd_remove->>cmd_remove: git fetch origin --prune loop For each worktree cmd_remove->>is_worktree_merged: Check if merged is_worktree_merged->>is_worktree_merged: Check uncommitted changes is_worktree_merged->>is_worktree_merged: Check untracked files is_worktree_merged->>gh CLI: gh pr list --head branch --state merged gh CLI-->>is_worktree_merged: PR state is_worktree_merged-->>cmd_remove: merged status alt If merged and clean cmd_remove->>remove_worktree: Remove worktree cmd_remove->>cmd_remove: Delete branch else If not merged or dirty cmd_remove->>cmd_remove: Skip with warning end end cmd_remove-->>User: Summary (removed/skipped counts)```## Additional Considerations-**Backward Compatibility:** The existing `clean --merged` functionality should remain unchanged to avoid breaking existing workflows
-**Error Handling:** Ensure proper error messages when `gh` CLI is not installed or not authenticated
-**Safety Checks:** Maintain existing safety checks (uncommitted changes, untracked files) to prevent data loss
-**Dry Run Mode:** The `--dry-run` flag allows users to preview what would be removed before committing to the operation
-**Confirmation Prompts:** Respect the `--yes` flag for non-interactive mode, otherwise prompt for each worktree removal
Activity
traycerai commentedon Jan 7, 2026
Plan
Observations
The codebase already implements merged branch cleanup functionality in
cmd_clean --merged(file:bin/gtr lines 926-1024), which uses GitHub CLI to detect merged PRs and removes associated worktrees. However, this feature is undocumented in the README and the user is requesting it under thermcommand instead. The current implementation checks for uncommitted changes, untracked files, and usesgh pr listto verify merge status before removal.Approach
Add a
--mergedflag to the existingcmd_removefunction to enable bulk removal of worktrees with merged branches. This provides a more intuitive UX (git gtr rm --merged) compared to the existingclean --mergedcommand. The implementation will reuse the merge detection logic fromcmd_cleanbut integrate it into thermcommand's workflow, maintaining consistency with existing flags like--delete-branch,--force, and--yes.Implementation Steps
1. Extend
cmd_removeFunction Flag ParsingLocation: file:bin/gtr lines 338-368
Add
--mergedflag handling in the argument parser:merged_modevariable (default: 0)--mergedflag to setmerged_mode=1--mergedis set, ignore theidentifiersrequirement check (line 370-373)--dry-runflag support for preview mode2. Extract Merge Detection Logic into Reusable Function
Location: file:lib/core.sh (new function at end)
Create a new function
is_worktree_merged()that encapsulates the merge detection logic:worktree_path,branch_name,main_branchgit diff --quietandgit diff --cached --quietgit ls-files --others --exclude-standardgh pr list --head "$branch" --state merged --json state --jq '.[0].state'3. Implement Merged Mode in
cmd_removeLocation: file:bin/gtr lines 380-432
Add conditional logic after line 378 (after resolving repo context):
merged_mode=1, perform bulk merged branch removal:ghCLI is available (similar tocmd_cleanlines 930-941)git fetch origin --prunecurrent_branch "$repo_root"$base_dir/${prefix}*is_worktree_merged()functionmerged_mode=0, use existing identifier-based removal logic4. Update Help Text
Location: file:bin/gtr lines 1431-1435
Extend the
rmcommand documentation:--mergedflag description: "remove all worktrees with merged PRs (requires gh CLI)"--dry-run, -nflag description: "preview what would be removed"git gtr rm --merged --yesfor non-interactive cleanup5. Update README Documentation
Location: file:README.md lines 216-226
Enhance the
git gtr rmsection:--mergedto the options list with descriptiongit gtr rm --merged --dry-runto preview merged branchesgit gtr rm --merged --yesfor automated cleanup--mergedflag6. Update CHANGELOG
Location: file:CHANGELOG.md lines 7-10
Add to the
[Unreleased]section under### Added:--mergedflag forrmcommand to bulk remove worktrees with merged PRs (requires GitHub CLI)"--dry-runflag forrmcommand to preview removals"Sequence Diagram
sequenceDiagram participant User participant cmd_remove participant is_worktree_merged participant gh CLI participant remove_worktree User->>cmd_remove: git gtr rm --merged cmd_remove->>cmd_remove: Parse --merged flag cmd_remove->>gh CLI: Verify gh CLI available cmd_remove->>cmd_remove: git fetch origin --prune loop For each worktree cmd_remove->>is_worktree_merged: Check if merged is_worktree_merged->>is_worktree_merged: Check uncommitted changes is_worktree_merged->>is_worktree_merged: Check untracked files is_worktree_merged->>gh CLI: gh pr list --head branch --state merged gh CLI-->>is_worktree_merged: PR state is_worktree_merged-->>cmd_remove: merged status alt If merged and clean cmd_remove->>remove_worktree: Remove worktree cmd_remove->>cmd_remove: Delete branch else If not merged or dirty cmd_remove->>cmd_remove: Skip with warning end end cmd_remove-->>User: Summary (removed/skipped counts)Additional Considerations
clean --mergedfunctionality should remain unchanged to avoid breaking existing workflowsghCLI is not installed or not authenticated--dry-runflag allows users to preview what would be removed before committing to the operation--yesflag for non-interactive mode, otherwise prompt for each worktree removalImport In IDE
🤖 Prompt for AI Agents
Execution Information
Branch: main
Commit: 1fc840e
💡 Tips
Supported Commands (Inside Comments)
@traycerai generateto iterate on the previous version of the implementation plan.Supported Commands (Inside Description)
@traycerai ignoreanywhere in the ticket description to prevent this ticket from being processed.@traycerai branch:<branch-name>anywhere in the ticket description to specify the target branch for the implementation plan.Community
helizaga commentedon Jan 7, 2026
This already exists as git gtr clean --merged (added in #64). Does that cover your use case?
docs: document how to remove all merged worktrees