Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all stale branches from Github?

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?

like image 342
the_drow Avatar asked Aug 21 '16 14:08

the_drow


People also ask

Should I delete stale branches?

They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.

How do I delete unused branches?

Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

How do you remove all branches that are not on remote?

Remove All Local Branches not on Remote First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.


2 Answers

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())
like image 145
Luna Lovegood Avatar answered Sep 21 '22 13:09

Luna Lovegood


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.

like image 24
kfb Avatar answered Sep 18 '22 13:09

kfb