A command-line utility that automatically cleans up local Git branches whose pull requests have been merged on GitHub.
Over time, developers accumulate local Git branches from feature development and bug fixes. After a pull request is merged into main, the local branch becomes obsolete but remains in the repository, cluttering the branch list. Running git branch might show dozens of stale branches that no longer serve any purpose.
git-tidy solves this problem by:
- Scanning all local branches (excluding main/master)
- Checking the status of associated pull requests on GitHub via the API
- Identifying which branches have been merged
- Deleting those branches to keep the repository tidy
This saves developers the manual effort of tracking which branches are safe to delete.
git-tidy is written in Go. You need Go 1.21 or later.
# Clone the repository
git clone https://github.com/yourusername/git-tidy.git
cd git-tidy
# Build the binary
go build -o git-tidy main.go
# Optionally install to your PATH
go installTo tidy up dependencies:
go mod tidygit-tidy needs a GitHub token to query the GitHub API for pull request status. It automatically retrieves your token from the GitHub CLI (gh) configuration, so you don't need to manage tokens manually.
The easiest way to set up credentials is to authenticate with the GitHub CLI:
# Install gh if you haven't already
# macOS: brew install gh
# Ubuntu: sudo apt install gh
# See https://cli.github.com for other platforms
# Authenticate with GitHub
gh auth loginFollow the prompts to authenticate. Once complete, git-tidy will automatically use the same token.
git-tidy looks for a GitHub token in the following order:
- Environment variables:
GH_TOKENorGITHUB_TOKEN - gh config file:
~/.config/gh/hosts.yml(or%APPDATA%\GitHub CLI\hosts.ymlon Windows) - System keyring: Modern versions of
ghstore tokens in the OS keyring for better security
If you prefer to use an environment variable:
export GH_TOKEN=ghp_your_token_hereThe token needs repo scope to read pull request information from private repositories, or just public access for public repositories.
Run git-tidy from within a Git repository:
# Preview what would be deleted (recommended first step)
git-tidy --dry-run
# Actually delete merged branches
git-tidy| Option | Short | Description |
|---|---|---|
--dry-run |
-n |
Preview what would be deleted without actually deleting |
--help |
-h |
Display usage information |
$ git-tidy --dry-run
Found 5 local branches (excluding main/master)
feature/add-login -> PR #42 merged (would delete)
bugfix/fix-typo -> PR #38 merged (would delete)
feature/new-dashboard -> PR #45 not merged (state: open)
experiment/test-idea -> no PR found
feature/old-feature -> PR #30 merged (would delete)
Dry run complete. Would delete 3 branches.
Running without --dry-run will actually delete the merged branches:
$ git-tidy
Found 5 local branches (excluding main/master)
feature/add-login -> PR #42 merged, deleting...
bugfix/fix-typo -> PR #38 merged, deleting...
feature/new-dashboard -> PR #45 not merged (state: open)
experiment/test-idea -> no PR found
feature/old-feature -> PR #30 merged, deleting...
Deleted 3 branches.
- Branch Discovery: Runs
git branch --format=%(refname:short)to get all local branches, excluding main and master - Repository Identification: Extracts the owner/repo from the Git remote URL (supports both SSH and HTTPS)
- PR Status Check: For each branch, queries the GitHub API to find associated pull requests
- Merge Detection: Checks if the PR has a non-empty
merged_attimestamp - Cleanup: Deletes merged branches using
git branch -D
git-tidy works on:
- Linux
- macOS
- Windows
Credential retrieval adapts automatically to each platform's conventions.
MIT