Skip to content
← Writing

Deleting Local Stale Branches in Git

Cleaning up a Git repository by deleting the local branches whose remotes are gone — one command, plus what is actually safe to remove.

  • 2 min read
Skip to contents
Contents
Title card reading ‘Cleaning up stale branches in Git — Streamline your workspace’ on a mauve gradient.

In Git, it’s common to work with many branches, especially when collaborating on large projects. Over time, some of these branches may become stale, no longer needed or merged into the main branch. To keep your repository clean, you can delete these local stale branches. This guide will walk you through a handy one-liner command to automate the process.

What Stale Branches Cost You

Stale branches can clutter your repository, making it harder to navigate and manage your branches. Deleting them:

  • Reduces confusion by removing outdated work.
  • Keeps your local environment in sync with the remote repository.
  • Improves overall project organization.

The Command

Here’s a one-liner that fetches the latest updates from your remote repository, lists remote branches, and deletes any local branches that are no longer present on the remote:

git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

Breaking Down the Command

  1. git fetch -p:
    This command fetches updates from the remote repository and prunes any branches that have been deleted on the remote. The -p flag is what triggers the pruning.

  2. git branch -r:
    This lists all remote-tracking branches.

  3. awk '{print $1}':
    The awk command extracts just the branch names from the output.

  4. egrep -v -f /dev/fd/0 <(git branch -vv | grep origin):
    This part compares the remote branches with your local branches that track them. egrep -v -f /dev/fd/0 excludes branches that are still present on the remote, so we’re left with the stale branches.

  5. git branch -d:
    Finally, xargs passes the list of stale branches to git branch -d, which deletes them.

When to Use This Command

Run this command periodically, especially after merging and deleting branches on the remote repository. It’s a safe way to clean up your local environment without accidentally deleting branches that are still in use.

Making It Routine

By regularly deleting stale branches, you keep your Git workspace clean and manageable. The one-liner automates that cleanup, so your local branch list stays in sync with the latest state of your project.

  • git
  • version-control
  • development-tools
  • command-line
  • productivity
  • workflow

Comments