avatar
2 minutes read

Delete all local git branches in Linux, except for the master or main branch

Working locally may lead to accumulating many git branches, but most of the time, we only want to keep the "main" or "master" branch. To achieve this, run the following command

If you are using "main" as the primary branch


git branch | grep -v "main" | xargs git branch -D

Or you are using "master" as the primary branch


git branch | grep -v "master" | xargs git branch -D

 git branch - List all available local branches.

grep -v "main" (or) grep -v "master" - Exclude the main or master branch from the list of output branches.

xargs git branch -D - runs git branch -D  to all the branches inside the branch list.  

Comments