17

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?

Community
  • 1
  • 1
the_drow
  • 17,855
  • 25
  • 122
  • 190
  • 1
    I'd love to have this feature in Github. When you fork a repo with 100+ branches that's just clutter. – Kangur Mar 26 '17 at 19:46

3 Answers3

13

For the 2021 Github's web page, you can right click on the browser after opening the stale branches and run this in the browser java script console.


 var stale_branches = document.getElementsByClassName('color-text-danger');
 for (var i = 0; i < stale_branches.length; i++) {
 stale_branches.item(i).click();
 }

or as @MartinNowak has suggested

document.querySelectorAll('button[data-target="branch-filter-item.destroyButton"]').forEach(btn => btn.click())
Luna Lovegood
  • 1,922
  • 14
  • 30
  • This is cool, any way of only doing that for merged ones? – Luis Davim Nov 03 '21 at 11:37
  • 1
    you can automatically do that though -> https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/managing-the-automatic-deletion-of-branches – Luna Lovegood Nov 03 '21 at 13:09
  • 1
    `document.querySelectorAll('button[data-target="branch-filter-item.destroyButton"]').forEach(btn => btn.click())` – Martin Nowak Nov 12 '21 at 20:35
  • For those searching, this solution doesn't work for Gitlab, because after the first branch deletion, the page refreshes, and you are no longer on the Stale branches tab, but the Branches Overview tab. If that didn't happen, it would have worked with a minor modification to the string passed to 'querySelectorAll'. – ryanwebjackson Apr 29 '22 at 00:45
  • 2
    2022, need to update the selector and add a delay: `var stale_branches = document.getElementsByClassName('js-branch-delete-button'); for (var i = 0; i < stale_branches.length; i++) { stale_branches.item(i).click(); await new Promise(r => setTimeout(r, 500)); }` – victorlin May 25 '22 at 22:21
  • thanks, but I get the error `Uncaught SyntaxError: await is only valid in async functions, async generators and modules` when I tried. – Luna Lovegood May 26 '22 at 17:43
11

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.

kfb
  • 5,902
  • 6
  • 36
  • 48
  • 5
    I wrote a Python script that does exactly this. It lists all existing branches that are referenced by at least 1 closed PR and in none open PRs. https://github.com/simplesurance/utils/blob/master/git/stale_github_pr_branches.py – fho Sep 12 '18 at 15:31
1

I took a different tack from the other answers and just used good ol' git (and bash) to do the work:

  1. Retrieve list of all branches using git branch -r >~/branches.txt (after setting git config --global core.pager cat)
  2. Prune ~/branches.txt of ones I want to keep
  3. Call git push <ref> --delete <branch> on each one ...
for ref_branch in $(cat ~/branches.txt | head -n 5); do
    ref="$(echo "$ref_branch" | grep -Eo '^[^/]+')"
    branch="$(echo "$ref_branch" | grep -Eo '[^/]+$')"
    git push "$ref" --delete "$branch"
done

Note the use of | head -n 5 to give it a try with only 5 at a time. Remove that to let the whole thing rip.

This will likely work on most sh shells (zsh, etc.) but not Windows; sorry.

Neil C. Obremski
  • 17,127
  • 21
  • 70
  • 98