How can I delete all Git branches that have been merged into the main branch? Is there a single command to delete all merged branches at once, rather than deleting them individually?
You can delete all merged Git branches using a single command by combining Git’s branch listing and deletion capabilities. The most efficient approach uses git branch --merged with git branch -d in a pipeline, or you can use the git remote prune command for remote branches. There isn’t a single built-in Git command specifically for this purpose, but you can easily create a one-liner that accomplishes this task safely.
Contents
- Understanding Merged Branches
- Local Branch Deletion Methods
- Remote Branch Deletion Methods
- Safety Precautions
- Automated Scripts
- Alternative Tools
Understanding Merged Branches
Before deleting merged branches, it’s essential to understand what “merged” means in Git context. A branch is considered merged when all its commits have been incorporated into another branch (typically main or master). Git tracks this information through merge commits and the branch’s ancestry.
The Git documentation explains that you can identify merged branches using the --merged option, which shows branches that are reachable from the specified commit (usually the main branch).
Note: Branches that have been merged may still contain commits that aren’t in the main branch if they were created after the merge point. Always verify which branches you’re deleting before executing deletion commands.
Local Branch Deletion Methods
Basic Command Pipeline
The most straightforward approach uses a pipeline to list merged branches and delete them:
git branch --merged main | grep -v " main$" | xargs git branch -d
How this works:
git branch --merged mainlists all branches merged into maingrep -v " main$"filters out the main branch itselfxargs git branch -dpasses the remaining branch names to delete
Alternative One-Liner
Here’s another variation that’s commonly used:
git branch --merged | grep -v "^\*" | grep -v " main" | xargs -r git branch -d
This version:
- Excludes the currently checked-out branch (
^\*) - Excludes the main branch
- Uses
-rflag with xargs to handle empty outputs gracefully
Interactive Deletion
For safer deletion, you can use an interactive approach:
git branch --merged | grep -v " main$" | less
Review the list in less, then manually delete the branches you want to remove.
Remote Branch Deletion Methods
For remote branches (like those on GitHub, GitLab, etc.), you’ll need different commands:
Using Git Remote Prune
git remote prune origin
This removes stale remote-tracking references that no longer exist on the remote repository.
Manual Remote Branch Deletion
To delete specific remote branches that have been merged:
git branch -r --merged main | grep -v "origin/main" | sed 's/origin\///' | xargs -r git push origin --delete
Safety note: This command permanently deletes remote branches and cannot be undone.
Safety Precautions
Before running any bulk deletion commands, consider these safety measures:
Backup Your Repository
git clone --bare /path/to/repo /path/to/repo-backup
Dry Run Mode
Many commands support a dry run option. For example:
# See what would be deleted (local)
git branch --merged | grep -v " main$"
# See what would be deleted (remote)
git branch -r --merged main | grep -v "origin/main"
Exclude Important Branches
Always modify the grep patterns to exclude any branches you want to keep, such as:
- Development branches
- Feature branches in progress
- Release branches
- Backup branches
Check for Unmerged Work
Before deleting, verify that no important work exists in branches you’re about to delete:
# Check for uncommitted changes
git status
# Check for unpushed commits
git log --oneline @{u}..
Automated Scripts
Shell Script for Safe Deletion
Create a script like clean-merged-branches.sh:
#!/bin/bash
# Safe script to delete merged branches
# Usage: ./clean-merged-branches.sh [branch]
TARGET_BRANCH=${1:-main}
echo "Checking for merged branches into $TARGET_BRANCH..."
echo "=============================================="
# Show branches that would be deleted
BRANCHES_TO_DELETE=$(git branch --merged "$TARGET_BRANCH" | grep -v " $TARGET_BRANCH$" | grep -v "^\*")
if [ -z "$BRANCHES_TO_DELETE" ]; then
echo "No merged branches to delete."
exit 0
fi
echo "The following branches would be deleted:"
echo "$BRANCHES_TO_DELETE"
echo
read -p "Delete these branches? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "$BRANCHES_TO_DELETE" | xargs git branch -d
echo "Branches deleted successfully."
else
echo "No branches deleted."
fi
Make it Executable and Use
chmod +x clean-merged-branches.sh
./clean-merged-branches.sh main
Alternative Tools
Git Extensions
If you’re using Git GUI tools, they often have built-in branch cleanup features:
- GitKraken: Branch management with visual merge indicators
- SourceTree: Branch cleanup tools
- GitHub Desktop: Branch management interface
Package Managers
Some package managers provide Git utilities:
- git-extras: Includes
git-delete-merged-branchescommand - github/gh: GitHub CLI with branch management features
IDE Integration
Most modern IDEs provide branch management:
- VS Code with GitLens extension
- WebStorm Git tools
- IntelliJ IDEA Git branch operations
Conclusion
Deleting merged Git branches can be efficiently accomplished with single-line commands or simple scripts. The key approaches include:
- Local branches: Use
git branch --merged | xargs git branch -dwith appropriate filtering - Remote branches: Use
git remote prune originor push deletion commands - Safety first: Always run dry runs and create backups before bulk operations
- Automation: Create scripts with confirmation prompts for safer batch operations
- Alternative tools: Consider Git GUI tools or extensions for visual branch management
Remember that while Git doesn’t have a single built-in command for deleting all merged branches at once, the combination of existing Git commands with basic shell tools provides an efficient and safe solution. Always verify which branches you’re deleting and maintain proper backups of important repositories.