Created
October 17, 2012 13:42
-
-
Save senny/3905587 to your computer and use it in GitHub Desktop.
Github Branch cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
current_branch = `git symbolic-ref --short HEAD`.chomp | |
if current_branch != "master" | |
if $?.exitstatus == 0 | |
puts "WARNING: You are on branch #{current_branch}, NOT master." | |
else | |
puts "WARNING: You are not on a branch" | |
end | |
puts | |
end | |
puts "Fetching merged branches..." | |
remote_branches= `git branch -r --merged`. | |
split("\n"). | |
map(&:strip). | |
reject {|b| b =~ /\/(#{current_branch}|master)/} | |
local_branches= `git branch --merged`. | |
gsub(/^\* /, ''). | |
split("\n"). | |
map(&:strip). | |
reject {|b| b =~ /(#{current_branch}|master)/} | |
if remote_branches.empty? && local_branches.empty? | |
puts "No existing branches have been merged into #{current_branch}." | |
else | |
puts "This will remove the following branches:" | |
puts remote_branches.join("\n") | |
puts local_branches.join("\n") | |
puts "Proceed?" | |
if gets =~ /^y/i | |
remote_branches.each do |b| | |
remote, branch = b.split(/\//) | |
`git push #{remote} :#{branch}` | |
end | |
# Remove local branches | |
`git branch -d #{local_branches.join(' ')}` | |
else | |
puts "No branches removed." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bjoernbur have a look!