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.

Scale Your Backend - Need an Experienced Backend Developer?

We provide backend engineers who join your team as contractors to help build, improve, and scale your backend systems.

We focus on clean backend design, clear documentation, and systems that remain reliable as products grow. Our goal is to strengthen your team and deliver backend systems that are easy to operate and maintain.

We work from our own development environments and support teams across US, EU, and APAC timezones. Our workflow emphasizes documentation and asynchronous collaboration to keep development efficient and focused.

  • Production Backend Experience. Experience building and maintaining backend systems, APIs, and databases used in production.
  • Scalable Architecture. Design backend systems that stay reliable as your product and traffic grow.
  • Contractor Friendly. Flexible engagement for short projects, long-term support, or extra help during releases.
  • Focus on Backend Reliability. Improve API performance, database stability, and overall backend reliability.
  • Documentation-Driven Development. Development guided by clear documentation so teams stay aligned and work efficiently.
  • Domain-Driven Design. Design backend systems around real business processes and product needs.

Tell us about your project

Our offices

  • Copenhagen
    1 Carlsberg Gate
    1260, København, Denmark
  • Magelang
    12 Jalan Bligo
    56485, Magelang, Indonesia

More articles

Full-Time Engineer vs Backend Contractor — A Cost Breakdown for EU Startups

EU employment law makes full-time engineers significantly more expensive than the gross salary suggests — understanding the true cost of each model is the prerequisite to making this decision rationally.

Read more

Git Reflog: The Safety Net Most Developers Don't Know They Have

Reflog is a local log of every position HEAD has ever been in. It is the reason that almost nothing in Git is permanently irreversible — and most developers have never opened it.

Read more

Rails Callbacks — The Rules I Follow to Not Regret Them Later

Rails callbacks are one of the most powerful and most regretted features in the framework. The difference between callbacks that help and callbacks that haunt you is a small set of rules applied consistently.

Read more

Your Microservices Are Too Dependent on Each Other

High coupling between microservices negates the independence benefits the architecture is supposed to deliver. Recognizing coupling patterns — shared databases, synchronous chains, shared libraries with domain logic — is the first step to eliminating them.

Read more