Skip to content

Instantly share code, notes, and snippets.

@christo8989
Forked from ddddavidmartin/.gitconfig
Created July 1, 2020 00:10
Show Gist options
  • Save christo8989/a06bf73d344c2e0373059841b0fe71bd to your computer and use it in GitHub Desktop.
Save christo8989/a06bf73d344c2e0373059841b0fe71bd to your computer and use it in GitHub Desktop.
Git checkout alias that accepts regular expressions
[alias]
co = !"find_and_checkout_branch(){\
for last; do true; done ; \
pattern='^/.*/$' ;\
if [[ $# -eq 1 && $last =~ $pattern ]] ;\
then \
branch_pattern=`echo $last | sed -e 's/^\\///' -e 's/\\/$//'` ;\
branch=`git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/ | grep -E -i $branch_pattern | head -n1` ;\
if [[ $branch ]] ; then \
git checkout $branch ;\
else \
echo "No local branch found matching $branch_pattern. Checking remote branches." ; \
branch=`git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/remotes/ | grep -E -i $branch_pattern | head -n1 | sed -e 's|origin/||'` ;\
if [[ $branch ]] ; then \
echo "Found $branch. Open it now? [y]/n: " ;\
exec < /dev/tty ;\
read response ;\
if [ -z "$response" ] || [ "$response" = "y" ] ; then \
echo "git checkout $branch" ; \
git checkout $branch ; \
fi ;\
else \
echo "No remote branch found matching $branch_pattern." ; \
fi \
fi \
else \
git checkout $@ ;\
fi \
} ; find_and_checkout_branch"

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.

Example

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment