Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git cleanup old branches

Tags:

git

git-bash

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?

like image 291
opsb Avatar asked Jun 22 '12 08:06

opsb


People also ask

How do you clean old branches?

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).

Should I delete old git branches?

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.


1 Answers

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.

like image 124
Christopher Avatar answered Oct 12 '22 09:10

Christopher