<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Levente Bagi</title>
  <link href="http://levente.bagi.name/atom.xml" rel="self"/>
  <link href="http://levente.bagi.name/"/>
  <updated>2011-09-13T12:06:02+01:00</updated>
  <id>http://levente.bagi.name/</id>
  <author>
    <name>Levente Bagi</name>
    
  </author>

  
  <entry>
    <title>Switching Git Branches the Lazy Way</title>
    <link href="http://levente.bagi.name/blog/2011/08/23/switching-git-branches-the-lazy-way/"/>
    <updated>2011-08-23T21:55:00+01:00</updated>
    <id>http://levente.bagi.name/blog/2011/08/23/switching-git-branches-the-lazy-way</id>
    <content type="html">&lt;p&gt;Ever been tired of typing &lt;code&gt;git checkout my-awesome-feature&lt;/code&gt;?
You can set up an alias and an autocompletion, but you will still have to type
&lt;code&gt;git co my&amp;lt;TAB&amp;gt;&lt;/code&gt; and remember the beginning of the branch name.&lt;/p&gt;

&lt;p&gt;At &lt;a href=&quot;http://picklive.com&quot;&gt;work&lt;/a&gt;, we typically use long, descriptive branch names, and I can only remember
1 or 2 words that are in it.&lt;/p&gt;

&lt;p&gt;So I've written a little script that finds the correct branch based on
the keyword and switches to it. If there's any ambiguity it will ask me,
and I will have to enter a number.&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/838607.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;#!/usr/bin/ruby

# Switch to a git branch by typing only part of the branch name.
# If multiple branches found, prompts for a number.
#
# Type `b dev` instead of `git checkout development`
#
# See also: 
# https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

pattern = ARGV[0]

all_branches = `git branch`.split(/[\n\r]+/).map{|l|l.gsub(/^\* /,'')}.map(&amp;amp;:strip).reject{|l|l == ''}

if pattern
  matching_branches = all_branches.select{|b| b.include? pattern}
else
  matching_branches = all_branches
end

if matching_branches.empty?
  puts &amp;quot;No branch matching #{pattern}&amp;quot;
  choosable_branches = all_branches
else
  choosable_branches = matching_branches
end

branch = if choosable_branches.size == 1
  choosable_branches.first
else
  puts choosable_branches.each_with_index.map{|b,i| &amp;quot;#{i+1}. #{b}&amp;quot;}.join(&amp;quot;\n&amp;quot;)
  print &amp;quot;Enter the branch number &amp;gt; &amp;quot;
  selected = $stdin.gets.to_i - 1
  if selected &amp;gt;= 0 &amp;amp;&amp;amp; selected &amp;lt;= choosable_branches.size-1
    choosable_branches[selected]
  end
end

if branch &amp;amp;&amp;amp; branch.strip != ''
  puts &amp;quot;Checking out #{branch.inspect}&amp;quot;
  `git checkout #{branch}`
end
&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;



</content>
  </entry>
  
</feed>

