2

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?

Creative Magic
  • 3,143
  • 3
  • 28
  • 47
  • Possible duplicate of [Can you delete multiple branches in one command with Git?](http://stackoverflow.com/questions/3670355/can-you-delete-multiple-branches-in-one-command-with-git) – Makoto Dec 03 '16 at 01:31

1 Answers1

2

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
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    Something like: `git branch -lr | grep -v X | grep -v Y | xargs git push origin --delete` might do it in a one-liner – Philip Whitehouse Dec 03 '16 at 01:42
  • 1
    @PhilipWhitehouse: Yes but that's dangerous--there's no opportunity to review what it will do. That said, thanks for the xargs idea--I'll simplify my answer using that. – John Zwinck Dec 03 '16 at 01:52