Delete all branches except master or main branch
To delete all branches in your Git repository except master, simply issue the following command:
$ git branch | grep -v "master" | xargs git branch -D
If you want to delete branches such as master-prod, master-test or master-ui, you can adjust the RegEx used by the grep as follows:
$ git branch | grep -v " master$" | xargs git branch -D
Remove all Git branches but main
=======
To delete all branches in Git except main, simply replace the grep for master with a grep for main:
$ git branch | grep -v "main" | xargs git branch -D
$ git branch | grep -v " main$" | xargs git branch -D

Comments
Post a Comment