5

I'd like to create a git command that will delete any branches that have all commits included in the current branch e.g.

$ git branch
  groups
* master

$ git cleanup-branches
deleted groups # all commits are included in master

$ git branch
* master

How would I go about creating this?

opsb
  • 28,147
  • 19
  • 86
  • 97
  • Finding the branches is answered here: http://stackoverflow.com/questions/226976/how-can-i-know-in-git-if-a-branch-has-been-already-merged-into-master – Alex Wilson Jun 22 '12 at 08:14

1 Answers1

5

You can leverage git branch -d here, as it won't delete any branch not yet merged into your current branch:

git config --global alias.cleanup-branches \
'!git branch | grep -v "\*" | awk "{ print $1 }" | xargs git branch -d'

Just tried this locally and it worked, although it is a little terrifying to watch it work.

Christopher
  • 40,100
  • 10
  • 77
  • 96
  • ha! yeah, that's one way to do it, try and delete everything! Having said that it does seem like a reliable way to do it. – opsb Jul 10 '12 at 14:10