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.
Prerequisites
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.
Deleting multiple branches using native git commands
Git provides commands to batch-delete branches locally or remotely. Here's how:
Deleting multiple local branches
List merged branches: Identify branches that have already been merged into
main
.Terminalgit branch --merged mainFilter and delete: Use
xargs
to batch-delete branches, excludingmain
or any critical branches.Terminalgit branch --merged main | grep -v "main" | xargs -n 1 git branch -dExplanation:
git branch --merged main
: Lists branches merged intomain
.grep -v "main"
: Excludes themain
branch.xargs -n 1 git branch -d
: Deletes branches one by one.
Deleting multiple remote branches
For remote branches, the process is similar but requires interaction with the remote repository.
List remote branches:
Terminalgit branch -rDelete multiple branches:
Terminalgit push origin --delete branch_name1 branch_name2Note: Replace
branch_name1
andbranch_name2
with the names of the branches to delete. Alternatively, you can automate this using a script.
Deleting branches using graphite's cli
Graphite CLI simplifies branch management, particularly when working with stacks or multiple branches. Here's how:
Cleaning up merged branches
Sync from the trunk and clean up: Graphite provides the
gt sync
command to fetch updates, restack branches, and delete merged branches.Terminalgt syncWhat 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.
- Pulls the latest changes into the trunk (e.g.,
Batch-deleting branches in a stack
If you manage stacked branches, deleting all merged branches becomes seamless with Graphite.
Visualize branches: Use
gt log short
to list branches in the stack.Terminalgt log shortDelete merged branches: Sync with
gt sync
to clean up. Alternatively, delete a specific stack manually by checking out the stack and running:Terminalgt checkout stack_namegt delete
Additional tips
- 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.