Live streamJoin us for "State of code review 2024" on July 18thRegister today

Deleting multiple Git branches at once

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Branch clutter is a common issue in active repositories. Regularly deleting stale branches simplifies navigation and reduces confusion. This post will guide you through batch-deletion of branches using Git and Graphite, helping you maintain a clean and organized repo.

Callout: A tidy repository is easier to navigate, less confusing, and more performant.

  • Clarity: Removing outdated branches clears the way for new features and fixes.

  • Performance: Fewer branches can mean faster fetches and pulls.

  • Maintenance: Clean repositories are easier to maintain and onboard new developers.

Git doesn't offer a built-in command for deleting multiple branches in one go, but with a little command-line magic, you can achieve this efficiently.

  1. List All Branches: Begin by listing the branches you want to delete.

    git branch

  2. Filter and Delete: Use grep to filter branches and xargs to batch-delete.

    git branch | grep 'pattern' | xargs git branch -d

    Replace 'pattern' with the common identifier for your stale branches, like a ticket number or a feature prefix.

Graphite provides a more user-friendly way to handle bulk branch operations with its stack management features.

  1. Review Your Stacks: Check your stacks and decide which branches are outdated.

    gt stack list

  2. Batch Delete: Use gt branch delete to remove multiple branches.

    gt branch delete <branch1> <branch2> <branch3>

    List all the branches you want to delete in one command.

Graphite's CLI commands can greatly simplify batch operations like deleting branches, saving you time and potential errors.

Whether you prefer the manual control of Git commands or the streamlined simplicity of Graphite, regularly pruning your branches is a best practice. It helps keep your repository healthy, up-to-date, and easier for all contributors to navigate.

Explore further branch management techniques and best practices by visiting the official Graphite 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