clean-gone-branches
npx machina-cli add skill dceoy/ai-coding-agent-skills/clean-gone-branches --openclawClean Gone Branches Skill
Remove stale local branches that have been deleted from the remote repository, along with any associated worktrees.
When to Use
- After merging and deleting remote branches, to clean up local tracking branches.
- When
git branch -vshows branches marked as[gone]. - Periodic local repository maintenance.
Agent Compatibility
This skill is tool-agnostic and can be executed by Claude Code, OpenAI Codex CLI, GitHub Copilot CLI, or Gemini CLI.
Inputs
- None required. The skill operates on the current git repository.
Workflow
-
List branches to identify any with
[gone]status:git branch -vBranches with a
+prefix have associated worktrees and must have their worktrees removed before deletion. -
List worktrees that may need removal for
[gone]branches:git worktree list -
Remove worktrees and delete
[gone]branches:git branch -v | grep '\[gone\]' | sed 's/^[+* ]//' | awk '{print $1}' | while read branch; do echo "Processing branch: $branch" worktree=$(git worktree list | grep "\\[$branch\\]" | awk '{print $1}') if [ ! -z "$worktree" ] && [ "$worktree" != "$(git rev-parse --show-toplevel)" ]; then echo " Removing worktree: $worktree" git worktree remove --force "$worktree" fi echo " Deleting branch: $branch" git branch -D "$branch" done -
Report results: List which worktrees and branches were removed. If no branches are marked as
[gone], report that no cleanup was needed.
Outputs
- Console output listing removed worktrees and deleted branches.
- If no
[gone]branches exist, a message indicating no cleanup was needed.
Source
git clone https://github.com/dceoy/ai-coding-agent-skills/blob/main/skills/clean-gone-branches/SKILL.mdView on GitHub Overview
This skill removes stale local branches that have been deleted on the remote and eliminates any associated worktrees. It operates on the current git repository and helps keep your local refs tidy after PR merges. No inputs are required—just run it in the repo you want to clean.
How This Skill Works
The tool identifies [gone] branches using git branch -v, then checks for linked worktrees with git worktree list. It loops over the marked branches, removes any associated worktrees, and finally deletes the local branches with git branch -D, reporting each action as it proceeds.
When to Use It
- After merging and deleting remote branches to clean up local tracking branches
- When git branch -v shows branches marked as [gone]
- Periodic local repository maintenance to remove stale refs
- In multi-worktree repos to ensure no orphaned worktrees remain
- Before starting new feature work to avoid clutter
Quick Start
- Step 1: Inspect branches with git branch -v to spot [gone]
- Step 2: Run the loop that removes worktrees and deletes the gone branches
- Step 3: Verify cleanup with git branch -v and git worktree list
Best Practices
- Verify the branches are not active or needed before deletion
- List worktrees first to identify any associated ones
- Back up important branches or push to a backup remote if needed
- Run the cleanup in a clean workspace or a test clone first
- Review the console output to confirm which worktrees and branches were removed
Example Use Cases
- Cleaning a cloned repo after multiple PR merges to remove stale local refs
- Maintaining a busy feature-branch repository by removing gone branches and worktrees
- Cleaning up after a fork where remote branches were deleted
- Preparing a repo for a large release by removing orphaned worktrees
- CI/CD environments tidying up before deployment by pruning stale branches