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
  • 29,325
  • 19
  • 89
  • 99
  • 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
  • 42,720
  • 11
  • 81
  • 99
  • 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
  • To avoid negative emotions completely do a dry run first (add `--dry-run` to the last git command). It will just list what would be deleted – wi2ard Jun 28 '23 at 08:01