Skip to content

Instantly share code, notes, and snippets.

@softcraft-development
Created May 11, 2012 14:15
Show Gist options
  • Save softcraft-development/2659937 to your computer and use it in GitHub Desktop.
Save softcraft-development/2659937 to your computer and use it in GitHub Desktop.
A ruby shell script to switch git branches based on a regex
#!/usr/bin/env ruby
search = Regexp.new(ARGV[0])
output = `git branch`
matching = output.lines.select{ |l| l.match(search) }
if matching.size == 0
STDERR.puts "No branch matches #{ARGV[0]}"
elsif matching.size == 1
`git checkout #{matching[0].strip}`
else
puts "Multiple branches matched:"
matching.each do |m|
puts " #{m.strip}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment