Skip to content

Instantly share code, notes, and snippets.

@jamie
Created August 20, 2012 15:09
Show Gist options
  • Select an option

  • Save jamie/3405031 to your computer and use it in GitHub Desktop.

Select an option

Save jamie/3405031 to your computer and use it in GitHub Desktop.
Script for removing remote branches which are fully merged into master
#!/usr/bin/env ruby
branches = `git branch -a`.split("\n")
remotes = branches.select{|e| e =~ %r(remotes/origin) && e !~ /HEAD/}
remotes.map!{|e| e.match(%r(origin/(.*)))[1] }
locals = branches.select{|e| e !~ %r(/) }
locals.map!{|e| e.match(/[^* ]+/)[0] }
# ignore locally active branches
remotes.reject!{|r| locals.any?{|l| r == l }}
remotes.each do |branch|
if `git cherry -v origin/master origin/#{branch}`.empty?
if ARGV.include? '--check'
puts branch
else
`git push origin :#{branch}`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment