Cleaning up local branches in Git

Greg Foster
Greg Foster
Graphite software engineer

Over time, Git repositories can become cluttered with numerous outdated or stale branches that can complicate navigation and increase the risk of errors. This guide provides a comprehensive walkthrough on cleaning up local Git branches, including commands and strategies to manage branches efficiently.

Before diving into branch cleanup, it's important to understand the distinction between local and remote branches in Git:

  • Local branches are branches that exist in your local Git repository.
  • Remote branches are references to the state of branches on a remote repository (like one hosted on GitHub or GitLab).

To start the cleanup process, first list all the branches you have locally, run:

git branch

This command displays all local branches in your repository.

To delete a single local branch that you no longer need, run:

git branch -d <branch-name>

Use -d to delete the branch safely (Git prevents deletion if the branch has unmerged changes). If you are sure you want to delete the branch regardless of its merge status, use -D to force delete a branch:

git branch -D <branch-name>

Be caution. As the name suggests this command forcibly deletes the branch, potentially deleting unsaved work.

If you want to delete all branches except for the main branch, you can use the following command:

git branch | grep -v "main" | xargs git branch -D

Replace "main" with your primary branch's name if you are using a different naming convention. This command lists all branches, filters out the main branch, and deletes the rest.

To delete local branches that have already been merged into the current branch (commonly the main branch):

git branch --merged | grep -v "\*" | xargs git branch -d

This command skips any branches that contain unmerged changes.

To find and delete branches not modified in the last X days, you can use:

git branch --sort=-committerdate | while read branch; do
if [ "$(git log -1 --since='X days ago' -s $branch)" == "" ]; then
git branch -D $branch

Replace X with the number of days you wish to use as a cutoff. This command will keep all your branches that have been modified within that time limit.

Before cleaning remote branches, synchronize your branch list with:

git fetch --prune

This updates your local copy of the remote branch list and removes any references to branches that have been deleted remotely.

If a branch on the remote (like origin) has been deleted, you can clean up the local references:

git remote prune origin

This will remove all references to the remote branch that you may have locally. These references are called "remote tracking branches".

To delete a remote branch:

git push origin --delete <branch-name>

This will delete the branch from the remote repository.

  • Regularly pull changes: Regularly pull changes from the remote to keep your local branches up to date.
  • Consistent naming conventions: Use clear, consistent naming conventions for branches to make management easier.
  • Regular cleanup schedules: Establish regular intervals for branch cleanup to prevent clutter.
  • Review before deletion: Always review branches before deleting, especially when using bulk deletion commands.

For further reading on cleaning up branches in Git, see the official documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2