Reflect on your 2024 year in code

Deleting multiple Git branches at once

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

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


When working with Git, branches are often created for specific features or experiments. Once these branches are merged or no longer needed, cleaning them up becomes essential to maintain repository hygiene. Deleting branches one by one can be tedious, especially for teams managing multiple branches. This guide will walk you through batch-deleting branches using Git commands and Graphite's CLI.

Before you begin:

  • Ensure you have Git installed and configured on your system.
  • If using Graphite CLI, install it via the Graphite documentation.
  • Confirm that you have the necessary permissions to delete branches in the repository.

Git provides commands to batch-delete branches locally or remotely. Here's how:

  1. List merged branches: Identify branches that have already been merged into main.

    Terminal
    git branch --merged main
  2. Filter and delete: Use xargs to batch-delete branches, excluding main or any critical branches.

    Terminal
    git branch --merged main | grep -v "main" | xargs -n 1 git branch -d

    Explanation:

    • git branch --merged main: Lists branches merged into main.
    • grep -v "main": Excludes the main branch.
    • xargs -n 1 git branch -d: Deletes branches one by one.

For remote branches, the process is similar but requires interaction with the remote repository.

  1. List remote branches:

    Terminal
    git branch -r
  2. Delete multiple branches:

    Terminal
    git push origin --delete branch_name1 branch_name2

    Note: Replace branch_name1 and branch_name2 with the names of the branches to delete. Alternatively, you can automate this using a script.

Graphite CLI simplifies branch management, particularly when working with stacks or multiple branches. Here's how:

  1. Sync from the trunk and clean up: Graphite provides the gt sync command to fetch updates, restack branches, and delete merged branches.

    Terminal
    gt sync

    What this does:

    • Pulls the latest changes into the trunk (e.g., main).
    • Prompts you to delete any local branches that have been merged or closed.

If you manage stacked branches, deleting all merged branches becomes seamless with Graphite.

  1. Visualize branches: Use gt log short to list branches in the stack.

    Terminal
    gt log short
  2. Delete merged branches: Sync with gt sync to clean up. Alternatively, delete a specific stack manually by checking out the stack and running:

    Terminal
    gt checkout stack_name
    gt delete
  • Automation with scripts: For repetitive tasks, consider writing a Bash script to delete branches based on a condition (e.g., merged status or age).
  • Safety checks: Always double-check the list of branches before batch-deleting. Use the -d flag for safe deletion and the -D flag for forced deletion in Git.
  • Graphite CLI benefits: Using Graphite for branch management is ideal for teams leveraging stacked workflows or PR-focused development.

For more details on Graphite CLI's features, you can refer to the documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone