Skip to content

Instantly share code, notes, and snippets.

@jish
Created July 15, 2013 02:02
Show Gist options
  • Save jish/5997030 to your computer and use it in GitHub Desktop.
Save jish/5997030 to your computer and use it in GitHub Desktop.
Branch cleanup. Made this a private gist to begin with, but people keep asking me for a link to it. Horray for open source! (:
#!/usr/bin/env ruby
puts "Starting..."
branches = `git branch --merged`.split("\n").map { |branch| branch.strip }
pattern = (ARGV.shift || `whoami`).strip
branches.select! { |branch| branch.match(pattern) }
count = branches.size
puts "Deleting #{count} branches..."
branches.each do |branch|
if system("git branch -d #{branch}")
# Check if there is a remote branch present
if system("git branch -a | grep origin/#{branch}")
system("git push origin :#{branch}")
end
count = count - 1
puts "deleted. (#{count} left)"
else
abort "FAILED TO DELETE #{branch}"
end
end
@rapheld
Copy link

rapheld commented Jan 7, 2014

Maybe add
'git fetch && git remote prune origin` at line 4?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment