Delete all branches on a github repo

I forked the QGIS repo on GitHub a little while ago although one thing that bothered me was that it gave me copies of all the branches that the main QGIS git repo had.  This is understandable as it’s the way it works however I don’t really need all these branches in my forked copy of the repo as I don’t care about them.   I only care the ones I am working on, and I don’t want to see a big list of branches in my git fork that have nothing to do with me.

So the next question was how do I delete all the branches on the remote repo at GitHub. Well you would normally do:

git push origin :branch_name

although doing that by hand for each branch is, well, a pain in the butt! After a chat with a guy (strk) on the #qgis IRC channel who is more skilled at bash then me (I’m still a Linux noob) he came up with this:

git branch -r | grep origin/ | grep -v master | grep -v HEAD| cut -d/ -f2 | while read line; do git push origin :$line; done;

Very handy.

2 thoughts on “Delete all branches on a github repo

  1. This breaks with branch names containing a forward slash after ‘origin/’ (e.g., a branch named ‘origin/bugs/123’). It will try to delete a branch named ‘bugs’ instead of ‘bugs/123’.

    The problem is with the cut command:
    cut -d/ -f2

    To fix it, add a dash right after the ‘2’:
    cut -d/ -f2-

    The entire command becomes:

    git branch -r | grep origin/ | grep -v master | grep -v HEAD| cut -d/ -f2- | while read line; do git push origin :$line; done;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s