How to Delete All Local Git Branches in One Command
First, checkout to main (or any other branch you want to clear from upstream branches).
Now, let’s build the command, step by step.
- Check which branches were merged to the current branch:
Shell
git branch --merged- Filter out current branch from the output – it is marked by an asterisk (*):
Shell
git branch --merged | grep -v \*- Turn the columnar output into a space-separated string:
Shell
git branch --merged | grep -v \* | xargs- Feed these arguments to the deletion command (passed in the second argument of xargs):
Shell
git branch --merged | grep -v \* | xargs git branch -D