本文へ移動
cccskills
無料GitHub で公開日本語紹介

stale-worktree-cleanup

作業中の変更を誤って失わないよう、Git worktreeや対応する不要なPostgresデータベースを調査・分類し、確認後に整理するスキルです。

原文原文の説明を見る

Use when cleaning up old git worktrees, removing worktrees whose branches have merged or been abandoned, or dropping orphaned compdev_* Postgres databases. Triggers on "clean up worktrees", "delete stale worktrees", "worktrees piling up", "orphaned databases", "remove unused worktree".

trycompai/compスター 2,061(GitHub 2,061 + cccskills 0)
連携ツールGitHubPostgreSQL

実行スコア 未検証

インストール方法を見る

こんなときに便利

  • 古いworktreeを整理したいとき
  • 孤立した開発用DBを探したいとき
  • 削除前に未コミット変更を確認したいとき

日本語での紹介

できること

古いGit worktreeと、それに対応する開発用Postgresデータベースを調査し、状態に応じて整理候補を分類します。ブランチのマージ状況、プルリクエスト、未コミットの変更、未プッシュのコミットを確認し、残すものと確認が必要なものを区別します。候補と理由を一覧で示したうえで、承認されたworktreeとデータベースをまとめて削除し、作業後に残りを確認します。

こんなときに便利

worktreeが増えて整理したいときや、対応する開発用データベースが残っているか確認したいときに使えます。作業中の変更や未公開コミットを守りながら、削除候補を判断したい開発者にも向いています。

使い方の例

  • 「古いworktreeを整理する候補を調べて」
  • 「使われていないcompdevデータベースを確認して」
  • 「各worktreeの変更とプルリクエストの状態を一覧にして」

注意点

削除前に利用者の確認を求めます。変更のあるworktreeを強制削除する場合は明示的な承認が必要です。ローカルブランチは明示的に依頼されない限り削除しません。

この紹介文は、公開されている SKILL.md をもとに AI(OpenAI Codex)が作成しました。正確な仕様は下の原文を確認してください。

含まれるファイル(1)

  • SKILL.md7.0 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Stale Worktree Cleanup

Overview

This repo's .githooks/post-checkout creates a per-worktree Postgres database (compdev_<slug>) on every git worktree add. Git has no pre-worktree-remove hook, so dead databases pile up over time. This skill defines a safe, reversible process to reap both the worktree directories and their dangling databases together.

Core principle: never delete work the user might still want. Classify first, ask second, remove last.

When to Use

  • User says "clean up worktrees", "delete stale worktrees", "remove old worktrees", "worktrees piling up"
  • User mentions orphaned compdev_* DBs or running out of disk space from dead node_modules
  • Starting a new feature and noticing many old worktree dirs

When NOT to Use

  • User wants to remove ONE specific worktree (just run git worktree remove <path> + drop its DB directly — don't load the whole process)
  • User is actively working in a worktree (never touch active work)

Process

Step 1 — Inventory

Run these four commands (parallel-safe) and capture the output:

git worktree list --porcelain
gh pr list --author @me --state all --limit 50 --json headRefName,state,url,number
git branch --no-color | cat

Then query the DB for compdev_* databases. The management URL is the main worktree's DATABASE_URL with the database path swapped to postgres:

bun -e '
  import { Client } from "pg";
  const raw = require("fs").readFileSync("packages/db/.env", "utf8");
  const url = raw.match(/^DATABASE_URL=(.*)$/m)[1].replace(/^["\x27]|["\x27]$/g, "");
  const mgmt = url.replace(/\/[^/?]+(\?|$)/, "/postgres$1");
  const c = new Client({ connectionString: mgmt });
  await c.connect();
  const r = await c.query("SELECT datname FROM pg_database WHERE datname LIKE \x27compdev\\\\_%\x27 ORDER BY 1");
  for (const row of r.rows) console.log(row.datname);
  await c.end();
'

Step 2 — Classify each worktree

For each worktree (skip the main one):

SignalClassification
Branch merged to main AND clean working tree AND no unpushed commitssafe
PR is CLOSED (not merged)needs-confirm
Uncommitted changes OR unpushed commitsneeds-confirm
No matching PR, no merge, has local commitskeep (user may still be working on it)
Is the main worktreeskip

Gather per worktree:

  • cd <path> && git status --porcelain | wc -l — uncommitted changes count
  • cd <path> && git log @{upstream}..HEAD --oneline 2>/dev/null | wc -l — unpushed commits (0 if no upstream)
  • Branch → PR lookup from step 1

Step 3 — Present to user

Show a table and explicit recommendations. Example:

Path                                    Branch                              PR state  Changes  Recommendation
.worktrees/sale-45-…                    mariano/sale-45-…                   MERGED    0 / 0    ✅ safe to remove
.worktrees/old-experiment               mariano/scratch                     —         3 / 0    ⚠ uncommitted — confirm first
.worktrees/worktree-env-auto-link       mariano/worktree-env-auto-link      OPEN      0 / 0    ⏳ keep (PR open)

Orphan databases (no worktree dir):
  compdev_abandoned_feature
  compdev_old_migration_test

Then ask: "Remove the items marked ✅? Confirm by listing anything you want me to also nuke from the ⚠ / ⏳ set."

Step 4 — Remove confirmed items

For each worktree the user approved:

# 1. Remove the worktree dir (use --force only if user confirmed dirty-removal)
git worktree remove "<path>"           # clean case
git worktree remove --force "<path>"   # only after explicit user OK

# 2. Derive the slug and drop the database
slug=$(basename "<path>" | tr '[:upper:]' '[:lower:]' | tr '-' '_' | tr -cd 'a-z0-9_')
bun -e '
  import { Client } from "pg";
  const raw = require("fs").readFileSync("packages/db/.env", "utf8");
  const url = raw.match(/^DATABASE_URL=(.*)$/m)[1].replace(/^["\x27]|["\x27]$/g, "");
  const mgmt = url.replace(/\/[^/?]+(\?|$)/, "/postgres$1");
  const c = new Client({ connectionString: mgmt });
  await c.connect();
  await c.query(`DROP DATABASE IF EXISTS "compdev_'"$slug"'"`);
  await c.end();
  console.log("dropped compdev_'"$slug"'");
'

For orphan databases (no matching worktree dir), just drop them — no worktree to remove.

Do NOT delete the local branch unless the user explicitly asked. Branches can be recreated from origin cheaply; worktrees cannot.

Step 5 — Verify and report

git worktree list
# then re-run the compdev_* query from step 1

Report back:

  • Worktrees removed (paths)
  • Databases dropped (names)
  • Anything skipped and why
  • What's left (still-active worktrees)

Safety Rules

  • Never remove the main worktree. Its path is always the first line of git worktree list --porcelain.
  • Never --force without confirmation. Dirty worktrees can contain un-stashed work.
  • Never DROP DATABASE on anything not matching ^compdev_[a-z0-9_]+$. Never drop comp, postgres, or any production-looking name.
  • Never delete a local branch as part of cleanup unless the user explicitly asks. Orphaned branches are cheap; lost work isn't.
  • Never run this inside a worktree you're about to delete. cd to the main worktree first.

Common Mistakes

MistakeFix
Dropping the DB but leaving the worktree dirRun git worktree prune then git worktree remove
Removing the worktree but leaving the DB (accumulates orphans)Always do both in the same pass
Using hyphens in the DB nameThe hook slug rule is tr '-' '_' — always underscores
Running from inside a doomed worktreecd to the main worktree before starting the process
Using gh pr list on a branch with no PRMissing data is not "abandoned" — needs needs-confirm classification

Red Flags

If any of these show up mid-process, stop and ask the user:

  • A worktree has unpushed commits AND no PR → might be unreleased work
  • The classification returned >10 "safe to remove" items → unusual volume, double-check
  • git worktree remove errors with "working tree is not clean" → never retry with --force without explicit consent
  • A compdev_* name has weird characters or unexpected format → don't drop

Quick Reference

# List everything
git worktree list --porcelain
gh pr list --author @me --state all --limit 50 --json headRefName,state

# Per-worktree inspection
git -C <path> status --porcelain
git -C <path> log @{upstream}..HEAD --oneline 2>/dev/null

# Clean removal
git worktree remove <path>

# Dirty removal (requires user confirmation first)
git worktree remove --force <path>

# Database drop (run from main worktree)
# See the bun -e snippets above for the exact invocation

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

コメント

ログインなしで書き込めます。使ってみた感想、質問、動かなかった環境などを気軽にどうぞ。

まだコメントはありません。最初のコメントを書いてみませんか。

送信するとすぐに公開されます。メールアドレスや電話番号などの個人情報は書かないでください。URL は 2 つまでです。

0 / 1000 文字

このフォームは reCAPTCHA で保護されており、Google のプライバシーポリシーと利用規約が適用されます。

同じリポジトリのスキル

api-endpoint-contract

無料概要あり

APIエンドポイントの追加・変更で、DTO、入力検証、OpenAPI仕様、MCPツールの説明や公開範囲を揃え、エージェントからも扱いやすいAPIに整えます。

  • APIエンドポイントを追加したいとき
  • DTOと入力検証を整えたいとき
  • MCPツールとしてAPIを公開したいとき
trycompai/comp2,0612026年10月24日 更新

audit-design-system

無料概要あり

既存コードのデザインシステム利用状況を調べ、旧UI部品やlucide-reactのアイコンを@trycompai/design-systemの部品・Carbonアイコンへ置き換えるスキルです。

  • 旧UI部品を移行したいとき
  • アイコンをCarbonに置き換えたいとき
  • 画面のデザインシステム準拠を確認するとき
trycompai/comp2,0612026年10月24日 更新

audit-hooks

無料概要あり

指定されたファイルのフックやAPI利用を点検し、禁止パターンをSWRやapiClientなどの指定方式へ修正するための監査手順と実装規約をまとめています。

  • 既存コードのAPI利用を点検したいとき
  • 生のfetchをapiClientに置き換えたいとき
  • 更新処理をSWRのmutateにそろえたいとき
trycompai/comp2,0612026年10月24日 更新

audit-rbac

無料概要あり

NestJSのAPIとフロントエンドを調べ、権限チェックや監査ログのルール違反を修正するための確認項目を示します。

  • APIの権限チェックを点検したいとき
  • 画面操作の権限制御を確認したいとき
  • RBAC違反を修正したいとき
trycompai/comp2,0612026年10月24日 更新

audit-tests

無料概要あり

権限で表示や操作が変わるコンポーネントの単体テストを点検し、不足分の追加や失敗の修正を行って、管理者と閲覧専用ユーザーの動作を確かめます。

  • 権限別の単体テストを追加したいとき
  • 管理者と監査ユーザーの表示を検証したいとき
  • 失敗する権限テストを修正したいとき
trycompai/comp2,0612026年10月24日 更新

better-auth-best-practices

無料概要あり

Better Authを使ったTypeScript認証の設定を支援し、データベース接続、セッション管理、メール認証、OAuth、プラグインやセキュリティ項目を整理します。

  • Better Authで認証を導入したいとき
  • DBやORMアダプターを接続したいとき
  • セッションの保存方法を設定したいとき
trycompai/comp2,0612026年10月24日 更新

trycompai のスキルをすべて見る

このスキルの問題を報告する