sajad torkamani

Command

This is the command you want:

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

Let’s break it down to understand what each part does.

Breakdown of command

git branch

Lists all the local Git branches.

grep -v '^\*‘:

When you run git branch, Git will prefix your current branch with an asterisk (*).

The -v flag to grep means match all the lines that don’t match the given pattern ('^\*' in this case). So git branch | grep -v '^*' gives us all the local branches except the current branch (remember the current branch has the * prefix).

xargs git branch -d

Runs git branch -d for each line in the input (all the local Git branches except the current one) and so deletes the branches.

Use xargs git branch -D if you’re sure you want to delete unmerged branches too.