switch branch in git by partial name

If I have the following branches in git

remotes/origin/HEAD -> origin/master

I want to switch to a branch using –just– the number, even if that requires calling a script
For example:

  • Homesick with bash-it as submodule
  • switch_branch 1178

    and the script/solution should do the following

    1. git branch -a (find all branches local and remote in my repository)
    2. filter by the given parameter (‘1178’ above)
    3. extract the name of the branch that git can use
    4. switch to that branch

    What is the recommended way to do it without having to perform all these steps manually?

    I am using Mac OSX, if that matters here.

    update —
    bash-it (github.com/revans/bash-it) serves my purpose

    Welcome to Bash It!
    Here is a list of commands you can use to get help screens for specific pieces of Bash it:
      rails-help                  list out all aliases you can use with rails.
      git-help                    list out all aliases you can use with git.
      todo-help                   list out all aliases you can use with todo.txt-cli
      brew-help                   list out all aliases you can use with Homebrew
      aliases-help                generic list of aliases.
      plugins-help                list out all functions you have installed with bash-it
      bash-it-plugins             summarize bash-it plugins, and their installation status
      reference <function name>   detailed help for a specific function

  • sh.exe": emacs: command not found (Git bash Windows)
  • How to automate git version release of master branch?
  • Git first push: Sending email and Cannot allocate memory
  • How to merge a branch to another branch in GIT?
  • 'cannot open git-upload-pack' error in Eclipse when cloning or pushing git repository
  • Unable to chdir or not a Git archive
  • 3 Solutions collect form web for “switch branch in git by partial name”

    There are very few occasions where you’d want to checkout remotes/origin/*. They exist but for the purposes of this shortcut, let’s not worry about them. This will get you what you want on OSX:

    git config --global alias.sco '!sh -c "git branch -a | grep -v remotes | grep $1 | xargs git checkout"'

    You can then issue git sco <number> to checkout a branch that includes <number> but excludes “remotes”. You can change sco to be anything you’d like. I just picked it for “super checkout”.

    Of course this won’t work terribly well if you’ve got more than one branch that matches <number>. It should, however, be a decent starting point.

    Here’s the solution 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)/)?([A-Z]+-[1-9][0-9]*-)?${1}") )
    case ${#MATCHES[@]} in
      ( 0 ) echo "No branches matched '${1}'" ; exit 1  ;;
      ( 1 ) git checkout "${MATCHES[0]}"      ; exit $? ;;
    echo "Ambiguous search '${1}'; returned ${#MATCHES[@]} matches:"
    for ITEM in "${MATCHES[@]}" ; do
      echo -e "  ${ITEM}"
    exit 1

    I called it git-rcheckout (“r” for regex, for want of a better name) and placed it in my path (it’s a little too long to shoehorn into my .gitconfig.)

    It will attempt to match against local and remote branches (though only checks out locals), and will tolerate (IE disregard for the purposes of searching) some JIRA stylings, such as branches starting with common prefixes and things styled like JIRA ticket IDs.

    e.g. Typing this:

    git rcheckout this

    Should match things like


    But the regexes I’ve used are sufficiently tolerant that you could also do:

    git checkout JIRA-123

    To access:


    It defaults to searching for branch prefixes, but actually you can use regexes to do fancier things if desired, like so:

    git rcheckout '.*bran'
    git rcheckout '.*is-br.*h'

    I switched to git-flow workflow and happy about it since then.

    Git Baby is a git and github fan, let's start git clone.