Here is a solution that I came up with for myself.
[ ${#} -ne 1 ] && { echo -e "Please provide one search string" ; exit 1 ; } MATCHES=( $(git branch -a --color=never | sed -r 's|^[* ] (remotes/origin/)?||' | sort -u | grep -E "^((feature|bugfix|release|hotfix)/)?([AZ]+-[1-9][0-9]*-)?${1}") ) case ${#MATCHES[@]} in ( 0 ) echo "No branches matched '${1}'" ; exit 1 ;; ( 1 ) git checkout "${MATCHES[0]}" ; exit $? ;; esac echo "Ambiguous search '${1}'; returned ${#MATCHES[@]} matches:" for ITEM in "${MATCHES[@]}" ; do echo -e " ${ITEM}" done exit 1
I called it git-rcheckout
("r" for regex, without a better name) and put it in my path (it's too long to include shoehorn in my .gitconfig
.)
It will try to map local and remote branches (although it checks the locale) and will tolerate (ignoring IE for search purposes) some JIRA styles, such as branches starting with common prefixes, and things decorated as JIRA ticket identifiers.
eg. By entering this:
git rcheckout this
Things must fit like
this-branch feature/this-branch bugfix/JIRA-123-this-branch JIRA-123-this-branch remotes/origin/this-branch remotes/origin/feature/this-branch remotes/origin/bugfix/JIRA-123-this-branch remotes/origin/JIRA-123-this-branch
But the regular expressions that I used are tolerant enough, which you can also do:
git rcheckout JIRA-123
To access:
bugfix/JIRA-123-this-branch JIRA-123-this-branch remotes/origin/bugfix/JIRA-123-this-branch remotes/origin/JIRA-123-this-branch
By default, branch prefix search is used, but in fact you can use regular expressions to do more interesting things if you want, for example:
git rcheckout '.*bran' git rcheckout '.*is-br.*h'
source share