As projects progress, we may create and merge numerous branches. Over time, both local and remote repositories accumulate unnecessary branches. These “useless branches” not only clutter the repository but also cause operational confusion and waste unnecessary storage space. This article will guide you through safely and efficiently cleaning up these obsolete branches step by step.

Why Clean Up Branches?

  1. Repository Cleanliness: Reduce the number of branches to avoid the “branch ocean” and associated chaos.
  2. Risk Reduction: Unmerged branches may be accidentally deleted; cleanup reduces this risk.
  3. Space Savings: Deleting useless branches frees up storage space in both local and remote repositories.

Preparatory Work Before Cleaning

Before deleting branches, perform these checks to avoid accidental code loss:
1. Confirm Permissions: In team projects, verify with team members if you have deletion rights (especially for main branches or branches in use by others).
2. Check Branch Status: Use git branch to check for unmerged code (confirm with git status or git log <branch-name>).
3. Backup Important Branches: If unsure about a branch’s necessity, back it up locally first:
git branch <backup-branch-name> (e.g., git branch dev_backup).

Deleting Local Useless Branches

1. View Local Branches

Run git branch to list all local branches (the * indicates the current branch):

* master
  dev
  feature-a
  feature-b
  bugfix-c

2. Identify Branches Merged into the Main Branch

Assume the main branch is master (or main). Use git branch --merged master to list branches already merged into the main branch (these are generally safe to delete):

git branch --merged master

Example output (if dev, feature-b, and bugfix-c are merged into master):

  dev
  feature-b
  bugfix-c

3. Delete Useless Branches

  • Normal Deletion: git branch -d <branch-name> (only deletes merged branches; errors for unmerged branches).
    Example: Delete feature-b and bugfix-c:
  git branch -d feature-b
  git branch -d bugfix-c
  • Force Deletion: For unmerged branches needing immediate removal (only in emergencies), use git branch -D <branch-name> (may lose uncommitted code!).

Deleting Remote Useless Branches

1. Directly Delete Remote Branches

If a remote branch is useless (e.g., feature-b exists only remotely and is already deleted locally):
git push origin --delete <branch-name>
Example: Delete remote feature-b:

git push origin --delete feature-b

2. Clean Up Local “Dangling” Remote Tracking Branches

After deleting a remote branch, local tracking records may remain. Use git fetch --prune (shorthand git fetch -p) to remove local tracking branches that no longer exist remotely:

git fetch -p

Advanced Tips: Bulk Delete Merged Branches

1. Bulk Delete Locally

Filter branches merged into the main branch and delete them in bulk:

git branch --merged master | grep -v '^\*\|master\|main' | xargs git branch -d

2. Bulk Delete Remotely

List remote branches merged into the main branch, then delete them in bulk:

git branch -r --merged master | grep -v 'master\|main' | sed 's/origin\///' | xargs -I {} git push origin --delete {}

Precautions

  1. Confirm Branch Usage: In team projects, verify with members if a branch is still in use before deletion.
  2. Avoid Unmerged Branch Deletion: Unmerged branches may contain code others need. Check commit history with git log <branch-name> before deletion.
  3. No “Trash Can” for Deleted Branches: Git has no recycle bin. If accidentally deleted, attempt recovery via git reflog or third-party tools (low success rate).

Summary

Regularly cleaning up useless branches keeps repositories organized and operations efficient. The key is confirming branch status first, then deleting step by step. Remember: double-check before deletion to avoid accidental code loss!

Xiaoye