Today I Learned

Delete all local branches except the current one

From time to time it is necessary to delete all local branches. Assuming there are branches:

git branch --verbose

  a             2eb734f6 Update image tag
  b             2eb734f6 Update image tag
  first-branch  2eb734f6 Update image tag
  second-branch 2eb734f6 Update image tag
* staging       2eb734f6 Update image tag

This command will delete all branches except the current one:

git branch | grep -v `git symbolic-ref -q --short HEAD` | xargs git branch -D
Deleted branch a (was 2eb734f6).
Deleted branch b (was 2eb734f6).
Deleted branch first-branch (was 2eb734f6).
Deleted branch second-branch (was 2eb734f6).

An alias is handy:

alias gdal="git branch | grep -v `git symbolic-ref -q --short HEAD` | xargs git branch -D"