Zsh git command auto-complete add extra origin to the git branch name

autocompletegitzsh

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?

Best Answer

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)