The below co
alias allows you to pass in a basic regular expression (specified by a leading and ending /
) for checking out a recently worked on branch. If no local branch is found it searches the remote branches and prints a prompt to confirm a possible match. If the given branch name isn't a regex it falls back to normal checkout
behavior.
With regex:
git co /cool-feature/
Switched to branch 'spike/1234/cool-feature'
With regex, only matching a remote branch:
git co /cool-feature/
No local branch found matching cool-feature. Checking remote branches.
Found spike/1234/cool-feature. Open it now? [y]/n:
git checkout spike/1234/cool-feature
Branch spike/1234/cool-feature set up to track remote branch spike/1234/cool-feature from origin
Switched to a new branch 'spike/1234/cool-feature'
With regex, no matching branch:
git co /non-existent/
No local branch found matching non-existent. Checking remote branches.
No remote branch found matching non-existent.
Without regex:
git co spike/1234/cool-feature
It works as expected (at least how I expect it to work) with multiple arguments:
git co -b my-new-branch-here
git co -b my-new-branch-here parent-branch-here
If you provide more than one argument it doesn't use regular expression matching.
For convenience it helps to set up a function to call the alias. For example for Bash in ~/.bash_aliases
, which allows to simply call gco branch_pattern
to check out the given branch.
gco() {
if [[ ! -z "$1" ]]; then
git co "/$1/"
fi
}