Remove local stale branches
Wahid Abduhakimov
Posted on September 22, 2022
It's no secret that we end up with tons of unused, stale branches (either merged or not) in local git repository.
You might want to clean-up unused branches for both organization and storage purposes. But you don't want to delete branches one-by-one which would literally take hours (sometimes).
Here is how to remove local branch in bulk.
1. Remove git branches on *nix terminal
.
# this will delete only merged branches
git branch --merged | grep -v \* | xargs git branch -D
# this will delete all branches except the current one checked out
git branch | grep -v \* | xargs git branch -D
2. Remove git branches on Windows Powershell/Terminal
.
# this will magically all branches except dev, main, master and develop.
git branch | Select-String -Pattern '^(?!.*(dev|main|master|develop)).*$' | ForEach-Object { git branch -D $_.ToString().Trim() }
# this will only delete merged branches.
git branch --merged | Select-String -Pattern '^(?!.*(dev|main)).*$' | ForEach-Object { git branch -D $_.ToString().Trim() }
Yeah, it's that easy. Go ahead and give your local git some breathe 🙂.
💖 💪 🙅 🚩
Wahid Abduhakimov
Posted on September 22, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.