Last active
August 29, 2015 14:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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