Skip to content

Instantly share code, notes, and snippets.

@danielmcgrath
Created April 26, 2011 15:55
Show Gist options
  • Save danielmcgrath/942533 to your computer and use it in GitHub Desktop.
Save danielmcgrath/942533 to your computer and use it in GitHub Desktop.
Automatic git branch switching based on substrings
alias fb="ruby ~/find_branch.rb $1"
# I hate git autocompletion with the white hot intensity of a
# thousand burning suns. This works better for me.
req = ARGV[0]
branches = `git branch | grep '#{req}'`
branch_list = branches.split(' ')
if branch_list.length > 1
puts
puts 'Found the following branches: '
puts
branch_list.each_index do |i|
puts ' ' + i.to_s + '. ' + branch_list[i]
end
puts
print "Which one do you want? "
to_get = STDIN.gets.chomp.to_i
`git checkout #{branch_list[to_get]}`
elsif branch_list.length == 1
`git checkout #{branches}`
else
puts "No branch found"
end
@ryanmcgrath
Copy link

Fyi for any bash-newbies who'd visit this: put the first part in your ~/.bash_profile instead.

Other than that this is really awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment