Skip to content

Instantly share code, notes, and snippets.

@mbyczkowski

mbyczkowski/brdelete.sh

Last active Aug 29, 2015
Embed
What would you like to do?
Keep your git clean!
#!/usr/bin/env ruby
STDOUT.write("---+ Pruning origin... ")
`git remote prune origin`
STDOUT.write("DONE\n")
STDOUT.write("---+ Collecting a list of merged branches to delete... ")
branch_output = `git branch -a --merged remotes/origin/master | grep -v master`
list_of_branches_to_delete = branch_output.split("\n")
# make sure we don't delete special branches, e.g. deploy branch
list_of_branches_to_delete = list_of_branches_to_delete.select{ |b| !b.include?('deploy') }
remote_branches_to_delete = list_of_branches_to_delete.select{ |b| b.include?('/') }.collect{ |b| b.split('/').last }
local_branches_to_delete = list_of_branches_to_delete.select{ |b| !b.include?('/') }
STDOUT.write("DONE\n")
STDOUT.write("---+ Deleting remote branches... ")
remote_branches_to_delete.each do |branch|
STDOUT.write("\n | Deleting #{branch}...")
`git push --delete origin #{branch}`
STDOUT.write("DONE")
end
if remote_branches_to_delete.size > 0
STDOUT.write(" (Deleted #{remote_branches_to_delete.size} branches)\n")
else
STDOUT.write("DONE (Nothing to delete!)\n")
end
STDOUT.write("---+ Deleting local branches... ")
local_branches_to_delete.each do |branch|
STDOUT.write("\n | Deleting #{branch}...")
`git branch -d #{branch}`
STDOUT.write("DONE")
end
if local_branches_to_delete.size > 0
STDOUT.write(" (Deleted #{local_branches_to_delete.size} branches)\n")
else
STDOUT.write("DONE (Nothing to delete!)\n")
end
STDOUT.write("---+ Running Git Garbage Collection...")
`git gc 2>&1 /dev/null`
STDOUT.write("DONE\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.