AdamNYC
AdamNYC

Reputation: 20445

Is there a shortcut for git branch name?

I tend to have long branch names for git (e.g., step110_create_search_engine_to_replace_google).

How should I refer to it simply as step110 in checkout/commit statements?

Upvotes: 29

Views: 10906

Answers (6)

moeabdol
moeabdol

Reputation: 5059

On Unix-like systems you can.

git branch | cut -d ' ' -f 2 | tr -d '\n'

Upvotes: 0

Míng
Míng

Reputation: 2608

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

Upvotes: 4

snipsnipsnip
snipsnipsnip

Reputation: 2605

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

Upvotes: 2

alex
alex

Reputation: 490647

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.

Upvotes: 14

david
david

Reputation: 33637

I just want to add that this file usually already comes with git. You don't need to download it again. You just need to locate it and run it.

On my system (Centos OS) the following steps works:

$ locate completion.bash
/usr/share/doc/git-1.7.4.1/contrib/completion/git-completion.bash
$ source /usr/share/doc/git-1.7.4.1/contrib/completion/git-completion.bash

Obvioiusly as pointed out already it's better to add this line to your .bashrc file in your home directory, so that you don't need to repeat it everytime you open a new shell.

In my case I would add the last command to my .bashrc file

source /usr/share/doc/git-1.7.4.1/contrib/completion/git-completion.bash

Upvotes: 1

simont
simont

Reputation: 72717

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:

Upvotes: 35

Related Questions