Remove all git local branches except "main"

Ariel Mejia - Aug 24 - - Dev Community

Working locally would tempt to acummulate a lot of git branches locally, most of the time we want to keep only the "main" branch, to execute this just run:

git branch | grep -v "main" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

Explanation

  • The first section git branch list all the branches locally.
  • grep -v "main" filter out from the output branches list the main branch
  • xargs git branch -D runs git branch -D to all the branch items inside the branch list

Happy Coding!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player