Skip to content

Instantly share code, notes, and snippets.

@damiankloip
Created December 14, 2015 12:14
Show Gist options
  • Save damiankloip/9b4c38ddb41a66b137d9 to your computer and use it in GitHub Desktop.
Save damiankloip/9b4c38ddb41a66b137d9 to your computer and use it in GitHub Desktop.
Git branch del by prefix
#! /usr/bin/env ruby
# Ruby wrapper script to filter git branches by prefix, provide a confirmation
# prompt, then delete those branches. Without confirmation prompt you can easily
# do something like `git branch -D (git branch | egrep "^\s+core-" | tr -d ' ')`
# or `(git branch | egrep "^\s+core-") | xargs git branch -D`. This could be
# done with a bash script, but Ruby is pleasant to use.
prefix = ARGV.first
raise 'No prefix specified' if prefix.nil?
branches = `git branch`
filtered = branches.split.select { |b| b =~ %r{^\s*#{prefix}} }
puts 'Matched branches:'
puts ''
puts filtered
puts ''
prompt = 'confirm? [y/n]: '.freeze
print prompt
# Create a loop that we break out of on 'y'/'n' only.
while input = STDIN.gets.chomp.downcase
case input
when 'y'
# Just break, deal with branch deletion below.
break
when 'n'
# Exit the script on 'n'.
exit
else
# Was not 'y' or 'n', retry.
print prompt
end
end
puts ''
system('git', 'branch', '-D', *filtered)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment