This alias lets you quickly delete all local branches except the current one and prune remote-tracking branches that no longer exist on the remote.
Run this command in your terminal to create the alias:
git config --global alias.clean-branches '!git branch | grep -v "^\*" | xargs git branch -D && git fetch --prune'Once added, you can run:
git clean-branchesThis will:
- Delete all local branches except the one you’re on.
- Prune remote-tracking branches that were deleted on the remote.
| Part | Description |
|---|---|
git branch |
Lists all local branches |
grep -v "^\*" |
Excludes the currently checked-out branch |
xargs git branch -D |
Deletes each remaining branch forcefully |
git fetch --prune |
Removes references to deleted remote branches |