-1

Delete all branches except master branch

  1. Your all local branch will be deleted except main or master branch branch
  2. This command do not affect the remote server
luk2302
  • 55,258
  • 23
  • 97
  • 137
er.arunkushwaha
  • 138
  • 1
  • 5
  • 1
    Obviously this was already asked and answered extensively years ago... (which is also the first Google hit when searching for your question title) – luk2302 Oct 18 '22 at 06:22

1 Answers1

2

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

luk2302
  • 55,258
  • 23
  • 97
  • 137
er.arunkushwaha
  • 138
  • 1
  • 5