Skip to content

Instantly share code, notes, and snippets.

@asiansteev
Created September 1, 2015 22:52
Show Gist options
  • Save asiansteev/17b63e1249269fed4589 to your computer and use it in GitHub Desktop.
Save asiansteev/17b63e1249269fed4589 to your computer and use it in GitHub Desktop.
An old rake task to cleanup branches on Github that match master
# This script will delete all branches that have been merged to master.
#
# Usage:
# ruby cleanup_branches.rb
#
# Or to delete all remote branches:
# ruby cleanup_branches.rb remote
#
remote = '-r ' if ARGV.first == 'remote'
# Get an array of all remote branches that have been merged
branches = `git branch #{remote}--merged master`.split("\n")
branches.collect{|b| b.strip!; b.slice!('origin/')}
# Get rid of 'origin/HEAD -> origin/master' and 'origin/master' lines
branches.delete 'HEAD -> origin/master' if remote
branches.delete 'master'
# Get rid of current branch if deleting local branches
branches.reject!{|b| b[0] == '*'}
branches.each do |b|
p "Deleting #{'remote ' if remote}branch #{b}"
if remote
`git push origin :#{b}`
else
`git branch -d #{b}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment