Permalink
Browse files

Add script for deleting merged git branches

  • Loading branch information...
1 parent 39992b1 commit a76376d64fe83e6295f12f80a7f5747645645a5e @mmrobins committed Sep 27, 2012
Showing with 44 additions and 0 deletions.
  1. +44 −0 rmb.rb
View
44 rmb.rb
@@ -0,0 +1,44 @@
+#!/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

0 comments on commit a76376d

Please sign in to comment.