cleanup-github-worktree

Identifies and removes stale git worktrees, local branches, and associated GitHub issues.

2.5k|524|Updated May 29, 2019
One-click install
npx skills add https://github.com/TencentBlueKing/bk-ci --skill cleanup-github-worktree
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cleanup-github-worktree
Source: https://github.com/TencentBlueKing/bk-ci/tree/main/ai/skills/cleanup-github-worktree
Command: npx skills add https://github.com/TencentBlueKing/bk-ci --skill cleanup-github-worktree

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve?

Long-running development with git worktrees accumulates abandoned worktrees, stale local branches, and lingering GitHub issues that clutter the repository and consume disk space. This Skill provides a controlled, user-confirmed workflow to find worktrees inactive beyond a chosen threshold and clean them up in bulk.

Core Features & Use Cases

  • Stale Worktree Detection: Cross-platform scripts (PowerShell and Bash) list worktrees whose last commit or directory modification exceeds a 7, 14, or 30-day threshold.
  • Preview Before Deletion: Shows the candidate branch list and requires explicit user confirmation before any destructive action.
  • Batch Cleanup with Per-Branch Safety: For each candidate branch, closes the linked GitHub issue, removes the worktree, and deletes the local branch, skipping branches with uncommitted changes and continuing past individual failures.
  • Use Case: After a sprint, ask the assistant to clean up worktrees unused for over two weeks; review the previewed branch list, confirm, and receive a summary table of issues closed, worktrees removed, and branches deleted.

Quick Start

Ask the assistant to clean up git worktrees that have not been used for more than two weeks and confirm the previewed list.

Frequently Asked Questions about cleanup-github-worktree

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I clean up old git worktrees automatically?

Run the cleanup workflow, choose an inactivity threshold of 7, 14, or 30 days, and review the previewed list of stale worktrees. After confirmation, each candidate's GitHub issue is closed, the worktree removed, and the local branch deleted.

How to find git worktrees not used for a long time?

The included scripts parse git worktree list --porcelain output and check each branch's last commit timestamp via git log, falling back to directory modification time for detached HEADs. Entries older than the specified day threshold are listed as candidates.

Does the cleanup script work on Windows, macOS, and Linux?

Yes. A PowerShell script (cleanup-worktree.ps1) handles Windows, while a Bash script (cleanup-worktree.sh) covers macOS and Linux, including platform-specific date and stat command differences.

What happens to branches with uncommitted changes during cleanup?

Branches with uncommitted changes are skipped rather than deleted, and the final summary report marks them as skipped. Individual branch failures do not interrupt the rest of the batch.

Why must the cleanup run on the master branch?

The workflow checks git branch --show-current before starting and aborts if the current branch is not master. This prevents deleting the worktree or branch you are actively working in.