We have a lot of branches that are inactive (the newest is 7 months old, the oldest is two years ago).
I'd like to remove all of those branches in bulk from the remote if no PR is still open for them.
Should I be using Github's API? Should I be using git using snippets like those provided in this StackOverflow question?
Is there some Github functionality I'm not familiar with that can help organize our repository?
You can certainly achieve this using the GitHub API, but you will require a little bit of fiddling to do it.
First, use the list pull requests API to obtain a list of open pull requests. Each item in this list contains a ["head"]["ref"]
entry which will be the name of a branch.
Now, using the get all references API, list all of the references in your repository. Note that the syntax for branches in the Git Data API is slightly different than the one returned from the pull request API (e.g. refs/heads/topic
vs. topic
), so you'll have to compensate for this. The references API also returns tags unless you search just the refs/heads/
sub-namespace, as mentioned in the docs, so be aware of this.
Once you have these two lists of branch refs, it's simple enough to work out which branches have no open pull requests (don't forget to account for master
or any other branch you wish to keep!).
At this point, you can use the delete reference API to remove those branch refs from the repository.