opsb
opsb

Reputation: 30211

git cleanup old branches

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?

Upvotes: 5

Views: 1041

Answers (1)

Christopher
Christopher

Reputation: 44244

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.

Upvotes: 5

Related Questions