5

After I clone a git repository locally, I want to switch branch to, says, 'ABCD'.

$ git branch -a
* master
  remotes/origin/ABCD
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

$ git checkout origin/ABCD   #### <- Here is the problem!
Note: checking out 'origin/ABCD'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at f2bf54a... Clean up README.md

When I press tab after git checkout, for some reason the autocompletion always starts with origin/ and hence a warning message of detached HEAD state.

How can I make the autocomplete not to add 'origin' at the beginning?

  • I don't see a completion issue here: you don't have a local branch ABCD, so it's normal for zsh to offer only master and origin/… as completions. Why did you expect other completions? To create a local branch, run git checkout -t origin/ABCD or git branch -t ABCD origin/ABCD. – Gilles Mar 2 '15 at 3:25
  • If I switch to bash and tried git checkout <TAB> I will see ABCD instead even though ABCD is not a no local branch. I think it is a behavioural difference. – Anthony Kong Mar 2 '15 at 3:32
  • Did you find a solution? This annoys me too and means I need to either manually type the branch or go back and delete the origin part. Are we doing it the wrong way? – Asta May 12 '15 at 13:16
3

I found a great plugin for zsh that you can use. If you are using oh-my-zsh then its called gitfast or if just using zsh then you can follow the instructions on his blog article.

As the author details there are in fact quite a lot of git completion issues and his efforts are to resolve them all. This is one issue that now works like the way it does in bash.

https://felipec.wordpress.com/2013/07/31/how-i-fixed-git-zsh-completion/

To enable in oh-my-zsh edit your .zshrc and change the plugins line to add gitfast like so

plugins=(git gitfast)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.