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
Explanation
- The first section
git branchlist all the branches locally. -
grep -v "main"filter out from the output branches list the main branch -
xargs git branch -Drunsgit branch -Dto all the branch items inside the branch list
Happy Coding!
Top comments (6)
Great to see how easy ‘xargs’ can make things!
Curious if there’s an easy way to also filter out other default branch names like “master”
You can use
grep -Ev 'main|master'theEadds "extended regex" and themain|masteris regex for "main or master".You can also add other regex patterns like "branches that begin with foo-" for example.
egrep for advanced regex.
nice
gjfg j gfj gfj fgj gfj fgjfg j fgj fgj
Some comments may only be visible to logged-in visitors. Sign in to view all comments.