Skip to content

Instantly share code, notes, and snippets.

@bobpattersonjr
Created March 9, 2011 15:57
Show Gist options
  • Save bobpattersonjr/862446 to your computer and use it in GitHub Desktop.
Save bobpattersonjr/862446 to your computer and use it in GitHub Desktop.
git branch switching script
#!/usr/bin/perl
#March 9th 2011
#fisrt pass at branch switch script
#author bob patterson <bob at bobpattersonjr dot com>
sub trim($);
print "fetching all branchs and tags\n";
`git fetch;git fetch --tags`;
$new_branch = trim($ARGV[0]);
$non_branch_test = `git branch|tr -d ' '|grep -x $new_branch`;
$branch_test = `git branch|tr -d ' '|grep -x '*$new_branch'`;
$non_branch_test = trim($non_branch_test);
$branch_test = trim($branch_test);
if('*'.$new_branch eq $branch_test){
print "you are on branch ".$new_branch."\n";
`git pull`;
}elsif($new_branch eq $non_branch_test){
print "branch ".$new_branch." already exists, switching to it and pulling\n";
`git checkout $new_branch`;
`git pull`;
}else{
print "creating ".$new_branch." from origin branch\n";
$tracking = 'origin/'.$new_branch;
system('git checkout -b '.$new_branch.' '.$tracking);
}
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment