Notes of Maks Nemisj

Experiments with JavaScript

Removing branches in git which are no longer in the GitHub (git)

Currently I’m working on a project which uses GitHub. Here we work with feature branches, which means every feature gets its branch and every branch has to go through the pull request and then merged back to the main line. Whenever branch is merged back it get’s deleted in the GitHub with button “Delete branch”. GitHub allows to restore branches, so we try to keep branches list as short as possible.

I’m working with a command line and after a while of such workflow git branch becoming bigger and bigger while git branch -r remains small. Sure, it’s possible to do git branch -d {branch_name}, but after a while I will have again the full list.

To fix this manual routine I’ve created a script which will cleanup all the branches which are available locally but already removed from the remote. This is a python script and working only for python 2.7 https://raw.githubusercontent.com/nemisj/git-removed-branches/master/git-removed-branches.py

By default this script will not remove any local branches, but only will list branches to be removed. To actually perform the deletion --prune flag must be specified.

One more trick: If you place this script inside the $PATH variable, then you will be able to run it as git command

git removed-branches

I found this trick at http://thediscoblog.com/blog/2014/03/29/custom-git-commands-in-3-steps/

NPM

If you prefer node.js and npm, you can install git-removed-branches of the script via npm

$ npm install -g git-removed-branches

JS source is available at github: https://github.com/nemisj/git-removed-branches

, , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.