Creative Magic
Creative Magic

Reputation: 3141

Delete all git branches but X and Y

I've inherited a project that has over 800 branches, each having hundreds of commits and thousands of changes. The branches were created to isolate and fix small issues in the project, but weren't deleted afterwards.

Now trying to compare branches in GitHub takes forever, and deleting branches one by one is mind-numbing.

Can I tell git/github to delete all branches other than X, Y, Z so I keep only three?

Upvotes: 2

Views: 407

Answers (1)

John Zwinck
John Zwinck

Reputation: 249133

There's no feature for this in the GitHub UI, but it's easy enough with a little shell scripting.

First, list all the remote branches into a file:

git branch -lr > branches.txt

Then edit that file, removing the names of branches you do not want to delete, plus any header/footer information.

Then feed the branch names to git and delete them remotely:

xargs git push origin --delete < branches.txt

Upvotes: 2

Related Questions