git
delete branches
merged branches
git cleanup
version control

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:

bash
git branch --merged

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:

plaintext
feature/user-auth
bugfix/login-issue

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:

bash
git branch -d branch-name

Keep in mind:

  • The -d option 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:

bash
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

This command:

  1. Lists merged branches, excluding the currently checked-out branch (grep -v "\*").
  2. Pipes them through xargs, which calls git branch -d for 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:

bash
git fetch origin

Prune Remote-tracking Branches

Remove any remote-tracking branches that no longer exist on the remote with:

bash
git remote prune origin

Delete Remote Branch

Although remote branches need to be deleted individually, here's how you can delete one:

bash
git push origin --delete branch-name

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:

bash
for branch in $(git branch -r --merged | grep -v '\->' | grep -v 'main' | grep -v 'master'); do
    git push origin --delete ${branch#origin/}
done

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:

DescriptionCommand
List merged local branchesgit branch --merged
Delete a specific merged local branchgit branch -d branch-name
Batch delete merged local branches`git branch --merged \grep -v "*" \xargs -n 1 git branch -d`
Fetch latest from remotegit fetch origin
Prune remote-tracking branchesgit remote prune origin
Delete a remote branchgit 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:

bash
git tag backup-branch-name branch-name

or

bash
git checkout branch-name
git checkout -b backup/branch-name

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.


Course illustration
Course illustration

All Rights Reserved.