Is there a shortcut for git branch name?

If you're on a Unix-like system (Linux, Mac OS X, perhaps others), there's the contrib/complete/git-completion.bash bash auto-complete ruleset, which will let you auto-complete git commands (you can type git checkout step110<tab> and your shell will autocomplete the branch-name.

To activate this:

  • If you've got the git source, in contrib/complete/ there's a file git-completion.bash. Put that somewhere safe (like ~/.git-completion), and then add the following line to your ~/.bashrc file: source ~/.git-completion. Either restart your shell session or run source ~/.git-completion to get it running in the current shell session.
  • If you dont have the git source, you can get the script from here (github.com). Then follow the same instructions as above.

If you're lucky enough to be using zsh instead of bash, I know that oh-my-zsh has git autocompletion plugins (I'm not sure how to activate them without oh-my-zsh).

Sources:

  • mbuttu.wordpress.com
  • codethatmatters.com

Try this alias:

cb = "!checkoutbranch() { local branches=`git branch | grep -i $1 | tr -d '* '`; if [[ `echo \"$branches\" | wc -l | tr -d ' '` != 1 ]]; then echo \"Matched multiple branches:\"; git branch | grep --color -i $1; exit 1; fi; git checkout $branches; }; checkoutbranch"

Checkout the develop branch:

git cb dev

---- edit ----

A better version: https://gist.github.com/iwill/88f5835bfc4e58aa1a88


git symbolic-ref may help if you are too lazy to even press a TAB. You can create an alias to the branch.

$ # Define short name 's1'
$ git symbolic-ref refs/heads/s1 refs/heads/step110_create_search_engine_to_replace_google
$
$ # You can use short name 's1' afterwards
$ git reset --hard s1
$ git checkout -b s1-experiment s1
$
$ # Remove the short name (don't use branch -d here or the original branch gets deleted!)
$ git symbolic-ref -d refs/heads/s1

Remote branches can be referred in the same manner to save typing remote/TAB. (In this case, I recommend to prefix with refs/tags/ instead of refs/heads/ to prevent moving the remote ref accidentally)

$ git symbolic-ref refs/tags/base refs/remotes/github/a-very-long-named-remote-branch
$ git rebase -i base

Here is how I installed it on OS X...

Check if it's on your local system first. It seems MacPorts and Homebrew download it for you.

$ find / -name "git-completion.bash"

Otherwise, download it...

$ wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion

If you don't have wget, you can install it easily with Homebrew or use cURL.

$ vim ~/.profile

...or your editor of choice.

Then add...

source ~/.git-completion

If your autocompletion doesn't work automatically...

$ source ~/.profile

...and then you have Git autocompletion.

Tags:

Git