Skip to content

Instantly share code, notes, and snippets.

@alsmola
Last active August 29, 2015 14:09
Show Gist options
  • Save alsmola/6444fbd44c2538904b6c to your computer and use it in GitHub Desktop.
Save alsmola/6444fbd44c2538904b6c to your computer and use it in GitHub Desktop.
List git branches with a numerical index, then check a branch out by index
#!/usr/bin/env ruby
# To setup - copy this to a file named git-list in a directory on your path and
# chmod +x.
#
# To use - Type git list, and select the branch you'd like to check out.
@branches = Array.new
class String
def white
"\033[37m#{self}\033[0m"
end
def bg_green
"\033[42m#{self}\033[0m"
end
end
def list_branches
@branches = `git branch -l`.split("\n").map do |branch|
branch.strip
end
@branches.each_with_index do |branch, index|
if branch.include? "*"
puts "#{index + 1}.\t" + "#{branch.split("*")[1].strip}".bg_green.white
else
puts "#{index + 1}.\t#{branch}"
end
end
end
def checkout_branch(branch_index)
`git checkout #{@branches[branch_index - 1]}`
end
list_branches
print "Checkout branch: "
branch_index = gets
if (branch_index =~ /^\d+$/)
puts "Checking out branch #{@branches[branch_index.to_i - 1]}..."
checkout_branch(branch_index.to_i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment