Skip to content

Instantly share code, notes, and snippets.

@simianarmy
Last active September 5, 2018 19:09
Show Gist options
  • Save simianarmy/9e48fbec2a00f2db353184b29bb6b409 to your computer and use it in GitHub Desktop.
Save simianarmy/9e48fbec2a00f2db353184b29bb6b409 to your computer and use it in GitHub Desktop.
Checkout by partial branch name
/**
* Checkout branch by partial branch name
* If only matches single branch, checks it out automatically.
* If matches multiple branches, displays selection user can pick from
*
* Usage:
* > gcobr JIRA-1432
* > gcobr partial-branch-name
*/
function gcobr {
IFS=$'\n'
branches=($(git branch | grep $1 | awk '{$1=$1};1'))
if [ $ZSH_VERSION ]; then
setopt sh_word_split
fi
count=0
for item in $branches
do
count=$((count+1))
echo "${count}. $item"
done
if [ $count -gt 1 ]; then
echo ""
vared -p "Select branch #: " -c selection
branch=$branches[$selection]
else
branch=$branches
fi
if [ -n "$branch" ]; then
echo "git checkout ${branch}"
git checkout $branch
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment