246 lines
5.2 KiB
Markdown
246 lines
5.2 KiB
Markdown
# cleanupGoneBranches
|
|
|
|
A small command-line utility written in Go that helps you clean up **local Git branches whose remote counterparts have already been deleted** ("gone" branches).
|
|
|
|
Instead of manually finding and deleting stale branches, this tool:
|
|
|
|
- Fetches and prunes your remote references
|
|
- Detects local branches whose upstream no longer exists
|
|
- Lets you interactively select which branches to delete
|
|
- Protects your current branch from accidental deletion
|
|
- Supports automatic cleanup and forced deletion
|
|
|
|
## Features
|
|
|
|
- ✅ Runs `git fetch --prune`
|
|
- ✅ Detects local branches with deleted remotes
|
|
- ✅ Interactive branch selection
|
|
- ✅ Range selection (`1,3-5`)
|
|
- ✅ Select all (`all`)
|
|
- ✅ Confirmation before deleting
|
|
- ✅ Never deletes the currently checked-out branch
|
|
- ✅ Optional forced deletion (`git branch -D`)
|
|
- ✅ Show-only mode
|
|
- ✅ Display equivalent manual Git commands
|
|
|
|
---
|
|
|
|
## Installation
|
|
|
|
### Pre-built binaries
|
|
|
|
Download the binary for your platform from the [latest release](https://git.allesda.app/Reproog/cleanupGoneBranches/releases)
|
|
|
|
The binary has no dependencies besides Git, but it must be **executable** and either called directly or available in your system `PATH`.
|
|
|
|
#### Windows
|
|
|
|
1. Download `cleanupGoneBranches-windows_amd64.exe`.
|
|
2. Rename it to `cleanupGoneBranches.exe` (optional, but enables the short command name).
|
|
3. Move it to a folder that is already in your `PATH` (e.g. `C:\Windows`), **or** add its folder to the `PATH`:
|
|
- Graphically: *System → Environment Variables → Path → Edit → New*
|
|
- Or in PowerShell (user scope, applies to new terminals):
|
|
|
|
```powershell
|
|
[Environment]::SetEnvironmentVariable(
|
|
"Path",
|
|
[Environment]::GetEnvironmentVariable("Path", "User") + ";C:\Tools\cleanupGoneBranches",
|
|
"User")
|
|
```
|
|
|
|
4. Verify:
|
|
|
|
```powershell
|
|
cleanupGoneBranches --help
|
|
```
|
|
|
|
> **Note:** If Windows SmartScreen warns about the unsigned executable, choose *More info → Run anyway*.
|
|
|
|
#### Linux
|
|
|
|
```bash
|
|
sudo mv cleanupGoneBranches-linux_amd64 /usr/local/bin/cleanupGoneBranches
|
|
sudo chmod +x /usr/local/bin/cleanupGoneBranches
|
|
|
|
# Verify
|
|
cleanupGoneBranches --help
|
|
```
|
|
|
|
Without root access, use `~/.local/bin` instead and make sure it is in your `PATH`.
|
|
|
|
#### macOS
|
|
|
|
```bash
|
|
sudo mv cleanupGoneBranches-darwin_arm64 /usr/local/bin/cleanupGoneBranches
|
|
sudo chmod +x /usr/local/bin/cleanupGoneBranches
|
|
xattr -d com.apple.quarantine /usr/local/bin/cleanupGoneBranches
|
|
```
|
|
|
|
The `xattr` command removes the Gatekeeper quarantine flag for this unsigned binary; without it, macOS may refuse to run it.
|
|
|
|
Alternatively, keep it anywhere and invoke it by full path:
|
|
|
|
```bash
|
|
/path/to/cleanupGoneBranches --help
|
|
```
|
|
|
|
### Build from source
|
|
|
|
```bash
|
|
git clone <repository>
|
|
cd cleanupGoneBranches
|
|
|
|
go build -o cleanupGoneBranches
|
|
```
|
|
|
|
Or install directly:
|
|
|
|
```bash
|
|
go install
|
|
```
|
|
|
|
The resulting binary is placed in your Go bin directory (`go env GOBIN` or `GOPATH/bin`), which must be in your `PATH` for the `cleanupGoneBranches` command to work globally.
|
|
|
|
|
|
---
|
|
|
|
## Usage
|
|
|
|
```bash
|
|
cleanupGoneBranches [options]
|
|
```
|
|
|
|
### Options
|
|
|
|
| Option | Description |
|
|
|---------|-------------|
|
|
| `--show` | Show gone branches only (don't delete anything) |
|
|
| `--all` | Select all gone branches automatically (still asks for confirmation) |
|
|
| `--force` | Use `git branch -D` instead of `git branch -d` |
|
|
| `--info` | Show the equivalent manual Git commands |
|
|
| `-h`, `--help` | Show help |
|
|
|
|
---
|
|
|
|
## Example
|
|
|
|
```
|
|
$ cleanupGoneBranches
|
|
|
|
Git fetch with prune...
|
|
|
|
Local branches with deleted remote (gone):
|
|
|
|
1) feature/login
|
|
2) bugfix/navbar
|
|
3) old-experiment
|
|
|
|
Enter your selection, e.g. 1,3-5 or all. Leave blank to cancel.
|
|
|
|
Selection: 1,3
|
|
|
|
The following branches will be deleted:
|
|
|
|
- feature/login
|
|
- old-experiment
|
|
|
|
Really delete? (y/N): y
|
|
|
|
Deleting branches...
|
|
|
|
Deleted branch feature/login
|
|
Deleted branch old-experiment
|
|
|
|
Done.
|
|
```
|
|
|
|
---
|
|
|
|
## Selection Syntax
|
|
|
|
You can select branches using:
|
|
|
|
| Input | Meaning |
|
|
|--------|---------|
|
|
| `1` | Delete branch #1 |
|
|
| `1,4,7` | Delete branches 1, 4, and 7 |
|
|
| `2-5` | Delete branches 2 through 5 |
|
|
| `1,3-6` | Mixed selection |
|
|
| `all` | Select every gone branch |
|
|
|
|
Leaving the prompt empty cancels the operation.
|
|
|
|
---
|
|
|
|
## Safe by Default
|
|
|
|
By default the tool uses:
|
|
|
|
```bash
|
|
git branch -d
|
|
```
|
|
|
|
which only deletes branches that Git considers safely merged.
|
|
|
|
If you want to force deletion, use:
|
|
|
|
```bash
|
|
cleanupGoneBranches --force
|
|
```
|
|
|
|
which uses:
|
|
|
|
```bash
|
|
git branch -D
|
|
```
|
|
|
|
---
|
|
|
|
## Show Gone Branches Only
|
|
|
|
```bash
|
|
cleanupGoneBranches --show
|
|
```
|
|
|
|
Useful if you just want to inspect stale branches.
|
|
|
|
---
|
|
|
|
## Delete Everything
|
|
|
|
```bash
|
|
cleanupGoneBranches --all
|
|
```
|
|
|
|
All gone branches are selected automatically, but you'll still be asked for confirmation before deletion.
|
|
|
|
---
|
|
|
|
## Manual Git Equivalent
|
|
|
|
Run:
|
|
|
|
```bash
|
|
cleanupGoneBranches --info
|
|
```
|
|
|
|
to display the Git commands that perform the same operations manually.
|
|
|
|
---
|
|
|
|
## Requirements
|
|
|
|
- Go 1.20+ (for building)
|
|
- Git installed and available in your `PATH`
|
|
- Must be executed inside a Git repository
|
|
|
|
---
|
|
|
|
## Notes
|
|
|
|
- The currently checked-out branch is never deleted.
|
|
- The tool automatically runs `git fetch --prune` before searching for gone branches.
|
|
- Branches are always sorted alphabetically.
|
|
- A confirmation prompt is shown before any deletion occurs.
|
|
|