git – Can I delete all the local branches except the current one?

Question:

I want to delete all branches that get listed in the output of …

… but keeping current branch, in one step. Is that possible? If so, how?

Answer:

Based on @pankijs answer, I made two git aliases:

To be added in ~/.gitconfig


And, as @torek pointed out:

Note that lowercase -d won’t delete a “non fully merged” branch (see the documentation). Using -D will delete such branches, even if this causes commits to become “lost”; use this with great care, as this deletes the branch reflogs as well, so that the usual “recover from accidental deletion” stuff does not work either.

Basically, never use the -force version if you’re not 300% sure you won’t lose anything important. Because it’s lost forever.

Leave a Reply