cleanup-branches
A fast CLI tool to delete local Git branches that have already been merged into a target branch.
Features
- Detects merged branches using commit graph analysis (
git2) - Skips the current branch and the base branch automatically
- Colour-coded output for quick scanning
--dry-runmode to preview changes before acting--forceflag to skip confirmation--ignorelist to protect specific branches
Installation
Binary release (Linux x86_64/aarch64)
Use the automated install script:
|
The default installation directory is ~/.local/bin. To install elsewhere, set the DIR variable:
| DIR=/usr/local/bin
Or download and extract a release manually from the releases page.
From source
Requires Rust (edition 2024, stable toolchain).
The binary will be at target/release/cleanup-branches. Copy it anywhere on your $PATH:
Optional: Alias and system-wide installation
Create a shorter alias (cb):
Add to your shell configuration (.bashrc, .zshrc, etc.):
Or install via cargo and create a system-wide symlink:
# Install with cargo
# Create symlink in /usr/bin (requires sudo)
# Optional: also symlink the short alias
Usage
cleanup-branches <repo-path> <base-branch> [OPTIONS]
Arguments
| Argument | Description |
|---|---|
<repo-path> |
Path to the Git repository |
<base-branch> |
Branch to check merged status against |
Options
| Option | Short | Description |
|---|---|---|
--force |
-f |
Delete without asking for confirmation |
--dry-run |
-d |
Show what would be deleted without making changes |
--ignore <LIST> |
-i |
Comma-separated branch names to keep |
--help |
-h |
Print help |
Examples
# Preview branches that would be deleted
# Delete merged branches, prompting for confirmation
# Delete without confirmation
# Keep specific branches even if merged
# Check against a different base branch
How it works
The tool opens the repository with git2 and fetches the latest state of the base branch from origin. It then iterates over all local branches and computes graph_ahead_behind(branch, base). Any branch with ahead == 0 is fully merged into the base — identical to what git branch --merged reports — and is a candidate for deletion.
Dependencies
| Crate | Purpose |
|---|---|
clap |
CLI argument parsing |
git2 |
Libgit2 bindings for repository operations |
anyhow |
Ergonomic error handling |