Stop Creating Branches You Never Clean Up
by Eric Hanson, Backend Developer at Clean Systems Consulting
The Branch Graveyard Problem
Run this on any team repository that's been active for more than six months:
git branch -r | wc -l
If that number is over fifty, you have a branch graveyard. Branches that were merged two years ago. Experiments that were abandoned. Features that were deprioritized and never came back. WIP branches from developers who left the company.
The damage is not just aesthetic. When a developer runs git branch -r to find an existing branch, they're scrolling through ghosts. When your CI system stores per-branch data, stale branches inflate storage and noise. When you're trying to understand what's actively being worked on by looking at branch names, you can't — because eighty percent of those branches are finished or abandoned.
Why Branches Don't Get Cleaned Up
The mechanics are slightly annoying. GitHub and GitLab offer an "auto-delete branch after merge" option, which handles the easiest case. But merged branches on the remote are only one category of stale branches. The others:
- Abandoned branches — started, never merged, developer moved on
- Long-paused branches — work that was deprioritized and never resumed
- Experimental branches — tried something, didn't commit to it
- Local branches that were pushed, then merged, but the local reference was never cleaned up
The auto-delete-after-merge setting in GitHub (Settings → General → "Automatically delete head branches") handles the merged case. Turn this on. It does not require a team decision — just do it.
What to Do With the Existing Graveyard
First, find merged branches that are safe to delete:
# List remote branches that have been merged into main
git branch -r --merged origin/main \
| grep -v 'origin/main' \
| grep -v 'origin/develop' \
| sed 's/origin\///'
# Delete them remotely (review the list first)
git branch -r --merged origin/main \
| grep -v 'origin/main' \
| grep -v 'origin/develop' \
| sed 's/origin\///' \
| xargs -I{} git push origin --delete {}
For unmerged stale branches, use last commit date to identify candidates:
# Remote branches with their last commit date, sorted oldest first
git for-each-ref --sort=committerdate refs/remotes/ \
--format='%(committerdate:short) %(refname:short)' \
| head -40
Any branch where the last commit is more than three months old and there's no open PR associated with it is a candidate for deletion. Check whether there's an open PR (gh pr list --head <branch>). If there's no open PR and no recent activity, it's safe to delete.
The Naming Convention That Helps
A branch naming convention that includes a ticket ID makes stale branch cleanup significantly easier. When you can see feature/PROJ-1234-add-oauth-login in the branch list, you can check whether PROJ-1234 is still open in your issue tracker. If it's closed and merged two years ago, the branch is clearly safe to delete. If it's marked "won't fix," same answer.
A common convention:
feature/PROJ-123-short-description
fix/PROJ-456-short-description
chore/PROJ-789-short-description
experiment/oauth-with-pkce # no ticket, explicitly experimental
The experiment/ prefix signals "this may never merge" — useful for honest communication and easier cleanup decisions later.
Pruning Local References to Deleted Remote Branches
Even after you delete remote branches, your local Git may still have references to them under refs/remotes/origin/. These are stale remote tracking references. Clean them up:
# Remove local references to remote branches that no longer exist
git fetch --prune
# Or configure Git to do this automatically on every fetch
git config --global fetch.prune true
After running git fetch --prune, git branch -r will only show branches that actually exist on the remote. Your tab completion becomes useful again.
Automating the Maintenance
For teams that won't maintain this manually, automate it. A GitHub Actions workflow that runs weekly:
name: Clean up merged branches
on:
schedule:
- cron: '0 9 * * 1' # every Monday at 9am UTC
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Delete merged remote branches
run: |
git branch -r --merged origin/main \
| grep -v 'origin/main' \
| grep -v 'origin/develop' \
| grep -v 'origin/HEAD' \
| sed 's/origin\///' \
| xargs -r -I{} git push origin --delete {}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
This handles the merged case automatically. The abandoned-branch case still requires periodic human review — you can't automate the decision to close out paused work without understanding project context.
The Standard Worth Setting
A repository's branches should reflect the current state of work. If you can look at the branch list and understand what's actively being built right now, the maintenance is working. If it takes a meeting to figure out which branches are relevant, the graveyard has gotten out of hand.
Enable auto-delete after merge, configure fetch.prune true globally, and schedule a quarterly fifteen-minute cleanup for unmerged stale branches. It's not glamorous maintenance — but it's the kind of hygiene that keeps a repository usable as it ages.