clean-branches — Claude Code command: delete merged and stale branches
clean-branches — a command that systematically removes merged and inactive branches from Git. Keeps your repository clean by deleting obsolete branches and syncing local state with the remote.
no license ★ 1,357 updated 2026-03-01
.claude/commands/clean-branches.mdWhat clean-branches does
clean-branches runs a complete branch audit: checks repository state, identifies the default branch, finds branches merged into main, and locates stale branches with no recent activity. It works step by step, analyzing first and asking for confirmation before deletion, making it safe to remove both local and remote-tracking branches.
The command uses standard Git tools: git branch --merged to find merged branches, git for-each-ref to analyze by commit date, and git remote prune to sync remote-tracking references. A built-in script protects critical branches—main, master, develop, staging, production—so they stay untouched.
It supports interactive mode with confirmation before each deletion, shows unmerged changes on a branch before removing it, and logs which branches were cleaned. If uncommitted changes exist, the command stashes them first.
What it helps with
- Delete branches that have been merged into main
- Remove stale branches inactive for more than thirty days
- Sync local remote-tracking branches with the remote server
- Prevent accidental deletion of critical branches
- Generate a report of deleted branches
Example prompts for the agent
- Clean up merged branches and synchronize the local repository state
- Find and remove branches with no updates in the last month
- Delete the
feature/old-featurebranch and sync with origin
How to install clean-branches
Manually: put the files in place
.claude/commands/clean-branches.mdCall it with /clean-branches. For all projects use ~/.claude/commands/
Manually: put the files in place
~/.codex/prompts/clean-branches.mdCall it with /prompts:clean-branches. Codex marks this format deprecated in favour of skills
Manually: put the files in place
.github/prompts/clean-branches.prompt.mdManually: put the files in place
.gemini/commands/clean-branches.tomlGemini stores commands as TOML: the prompt text goes into the prompt field
Questions about clean-branches
How do I install clean-branches in Claude Code?
Save the command file to .claude/commands/clean-branches.md. Claude Code will pick it up automatically when you load the project.
What does clean-branches do?
It scans your Git branches, identifies those merged into main and abandoned ones, removes them after verification, and syncs local state with the remote.
Which branches are protected from deletion?
The branches main, master, develop, staging, and production are protected and will not be deleted even if they are merged elsewhere.