How to remove every local branch except the current branch?
git branch | grep -v "^*" | xargs git branch -D
To remove every local branch except the currently checked out branch use git branch | grep -v "^*" | xargs git branch -D
. The command has three parts, git branch
lists all the local branches, grep -v "^*"
selects every branch except the current one and with xargs git branch -D
these branches are deleted.
Be careful, this command removes the main branch as well.
To do a dry run and see what branches would be removed use git branch | grep -v "^*"
.
git branch
* ab-test
feature-branch
main
test-branch
git branch | grep -v "^*" | xargs git branch -D
Deleted branch feature-branch (was 120f7ab).
Deleted branch main (was 120f7ab).
Deleted branch test-branch (was 120f7ab).
See git branch to learn more about it.