Automate git Branch Housekeeping


While my house is not so tidy, I like to keep my local and remote git branches pruned after development milestones. For years I’ve used a very simple bash function to delete a branch locally and remotely.

I finally had a few moments to develop this a little more and created a gist called which you can find here.

By default it iterates on your local branches, prompting you whether you want to remove it (and its remote if it has one). There are a few safeguards. It ignores:

  1. any branch that has the master, dev, qa, or staging substrings.
  2. the current branch.
  3. Prompts you before each deletion.

You can place it in your bin directory, then source it from your .bash_profile file. Then you get two main functions: delete_branches, and delete_branch, called like this:

$ delete_branches # or
$ delete_branch mybranch

You can also call the delete_branches function with the -r flag and it will iterate on remote branches, prompting you to delete them locally and on origin. Be careful not to delete remote branches that are someone else’s. Think before replying “y” to the prompt!

Enjoy your clean house.