How do I delete all Git branches which have been merged?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
By managing branches effectively, you can keep your Git repository clean and ensure a seamless development workflow. Frequently, teams work with multiple branches, and over time, many of these branches become obsolete once they are merged into the main branch (often main or master). Deleting merged branches is an effective way to declutter and lessen repository complexity.
This article highlights the steps needed to delete all Git branches that have been merged, providing you with the technical knowledge and commands necessary to manage your branches efficiently.
Identifying Merged Branches
Merging branches in Git is a common practice where changes from one branch are incorporated into another. Once a branch is fully merged, it's safe to delete it. Before jumping into deletion, however, it’s important to ensure that the branches you intend to delete are indeed merged.
Listing Merged Branches
To list all local branches that have been merged into the current working branch, use the following command:
This displays only those branches that have been successfully merged and are not the active branch (typically filtered out as the current HEAD).
Example
Assume you have the following branches in your repository:
- feature/user-auth
- bugfix/login-issue
- hotfix/payment-error
- feature/new-dashboard
Upon running git branch --merged, you might see:
These branches are deemed safe for deletion as they have been merged.
Deleting Merged Local Branches
Once merged branches are identified, you can proceed to delete them. Use the following command:
Keep in mind:
- The
-doption is used to delete the branch only if it has been merged. If the branch hasn’t been merged, Git will prevent its deletion. - For unmerged branches that you still want to delete, you can use
-D, but this should be done with caution as it forces deletion regardless of merge status.
Batch Deletion
To delete all merged branches in one go, use a loop within your terminal:
This command:
- Lists merged branches, excluding the currently checked-out branch (
grep -v "\*"). - Pipes them through
xargs, which callsgit branch -dfor each branch obtained.
Deleting Merged Remote Branches
Local deletion doesn’t affect remote branches. To clean up merged branches on the remote server, follow these steps:
Fetch Remote Changes
Firstly, ensure your repository is up to date with the remote:
Prune Remote-tracking Branches
Remove any remote-tracking branches that no longer exist on the remote with:
Delete Remote Branch
Although remote branches need to be deleted individually, here's how you can delete one:
Automating Remote Deletion
To automate deletion similar to local branch deletions, you can script the process. However, be very careful and ensure you have proper checks in place. A basic approach:
This must be run with care, and proper backups or verifications should be conducted before running such scripts.
Summary Table
Below summarizes key commands discussed:
| Description | Command | ||
| List merged local branches | git branch --merged | ||
| Delete a specific merged local branch | git branch -d branch-name | ||
| Batch delete merged local branches | `git branch --merged \ | grep -v "*" \ | xargs -n 1 git branch -d` |
| Fetch latest from remote | git fetch origin | ||
| Prune remote-tracking branches | git remote prune origin | ||
| Delete a remote branch | git push origin --delete branch-name |
Additional Considerations
Safety and Recovery
Git provides a safe choice with -d to prevent accidental deletions. Always verify if a branch can be safely removed by double-checking merge logs and any active developments not yet integrated.
Backup Branches
Before any deletion process, consider creating a backup via tags or another branch to ensure you can retrieve work if needed. Commands such as:
or
provide simple strategies for safeguarding important data.
In conclusion, diligent branch management minimizes clutter and potential confusion within your repository, easing navigation and contributing to efficient development practices. Employ these strategies wisely to maintain a streamlined and effective branching model in Git.

