Skip to content

Instantly share code, notes, and snippets.

@dgiunta
Created May 5, 2014 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgiunta/0bafa7439083c004a163 to your computer and use it in GitHub Desktop.
Save dgiunta/0bafa7439083c004a163 to your computer and use it in GitHub Desktop.
This little script uses the git-branch-report command but creates a report showing the commands to delete local branches that have already been merged into master.
#!/usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: git-safe-delete-branch-report [options]"
opts.on("--porcelain", "Output machine-readable output") do |v|
options[:porcelain] = v
end
end.parse!
safe_branches = `git-branch-report --porcelain`.split("\n").grep(/master/).reject {|l|
l =~ /^master/
}.map {|l| l.split(":")[0] }
if options[:porcelain]
puts safe_branches.join("\n")
else
if safe_branches.length > 0
puts "The following branches have been merged into master and thus are safe to delete:"
safe_branches.each do |branch|
puts <<-EOM
- #{branch}
git branch -D #{branch}
EOM
end
puts <<-EOM
Delete all branches with the following command:
git branch -D #{safe_branches.join(' ')}
EOM
else
puts "There are no branches that are safe to delete"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment