This script switches branches using regex matching. When more than one match is found, it prompts a
choice menu so you can pick the desired branch. If the regex filter is omitted, it prompts the most
recently switched branches. These are retreived from the repository's .git/recent-branches file,
which is populated by a post-checkout hook.
Partial branch name:
> git switch-regex develSwitched to branch 'develop'
Your branch is up to date with 'origin/develop'.
With metacharacters:
> git switch-regex ^ma[r-t].*r$Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Multiple matches:
> git switch-regex feature/coo[1] feature/coolBeans
[2] feature/cookie
Make your choice:
Remote-only branches are colored red. The script uses the hard-coded 'origin' as the remote name.
> git switch-regex[1] master
[2] develop
[3] my_feature
Make your choice:
Intead of switching to the matching branch, you can copy it to the clipboard by specifying using
the -c flag:
> git switch-regex -c develCopied 'develop' to the clipboard
You need to add the script directory to your PATH inside ~/.bashrc for Bash and ~/.zshrc
for Z Shell.
> echo 'PATH="~/git-switch-regex:$PATH"' > ~/.bashrcTo save recent branches to your repository's .git/recent-branches file, you need to set up a global
post-checkout hook. If you don't have a 'hooks' directory yet (check with
git config -l | grep core.hookspath), do the following:
> mkdir ~/.githooks && git config --global core.hooksPath ~/.githooksNow assuming ~/.githooks is your hooks directory, copy the post-checkout file from this repository
to ~/.githooks/ or alternatively, you can source it directly with:
echo "source ~/git-switch-regex/post-checkout" >> ~/.githooks/post-checkout