Skip to content

Instantly share code, notes, and snippets.

@lbland94
Created April 14, 2018 04:22
Show Gist options
  • Select an option

  • Save lbland94/49b553943c5c9125a60f8dc8cf07df22 to your computer and use it in GitHub Desktop.

Select an option

Save lbland94/49b553943c5c9125a60f8dc8cf07df22 to your computer and use it in GitHub Desktop.
Git tool to search for a branch. If one match for the string it checks out the branch. If more than one, it lists them.
#!/usr/local/bin/bash
str="/$*/"
str=$(echo "$str" | sed 's/ /\/ \&\& \//g')
str="print if $str"
res=$(git branch --all | perl -ne "$str")
res=$(echo $res | tr ' ' '\n' | cut -d/ -f3-)
matches=$(echo $res | tr ' ' '\n' | uniq -u | wc -l)
if (( $matches == 1 )); then
git checkout "$res"
else
echo $res | tr ' ' '\n'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment