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?
The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option. The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch. $ git branch -d release Deleted branch feature (was bd6903f).
They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With