Galder Zamarreño Galder Zamarreño - 8 months ago 59
Git Question

Disable auto-completion of remote branches in Zsh?

This is related to Disable auto-completion of remote branches in Git Bash?.

Anyone knows how to do the same with zsh?

Answer Source
zstyle :completion::complete:git-checkout:argument-rest:headrefs command "git for-each-ref --format='%(refname)' refs/heads 2>/dev/null"


Typing git checkout <Control-x><h> invokes _complete_help which exposes the internals of how zsh's completion system would act if you pressed TAB in the current context (instead of pressing <Control-x><h>). From this it is possible to see that zsh would invoke the __git_heads function to complete names of git branch heads. If you then type which __git_heads, you can see that these branch head names are obtained via:

_call_program headrefs git for-each-ref --format='"%(refname)"' refs/heads refs/remotes 2>/dev/null

Luckily for us, _call_program is designed specifically to allow the user to change the default behaviour. So the above zstyle command instructs zsh to use an alternative git for-each-ref ... invocation instead of the built-in one, and you can see that in the invocation above, I removed the refs/remotes parameter. The first parameter to zstyle is the completion context, and here it means "whenever the completion system is requesting completion for the headrefs tag when the user is completing an argument for git checkout. So this zstyle will only affect git checkout, and not any other git sub-commands.