How to add custom git command to zsh completion?

I’ve read a few guides on zsh completion, but I am still confused. In our development environment we have a custom Git command called git new-branch. I’d like zsh to auto-complete it for me after typing just git ne and a Tab. How can I do that?

  • git java wrapper - git pull never ends
  • Git “discrepancy”: `git diff` does not take commit messages into consideration but SHA-1 generation takes commit messages while computing SHA-1
  • Difference between “git checkout <filename>” and “git checkout -​- <filename>”
  • Combine two git folders
  • npm install not working on windows with git dependencies
  • Can I make git bisect display shortened commit SHAs?
  • Should I use my personal email in my ssh public keys?
  • How can I get the total lines committed today in git?
  • How to find which CRs in clearcase modified given lines in source code file (like 'git blame -L' does)?
  • Diff current file version and previous one remote repository
  • I really need help understanding how my git server is set up and what my branches are doing
  • Git shows files changed, but EGit does not
  • One Solution collect form web for “How to add custom git command to zsh completion?”

    The default git completion is extendible:

    Say you got your own git sub-commands (git will run a program `git-foo’
    when you run “git foo”) and you want “git f” to complete that sub
    commands name for you. You can make that sub-command known to the completion
    via the user-command style:

    % zstyle ':completion:*:*:git:*' user-commands foo:'description for foo'
    

    `user-commands’ is a list style, so you can add any number of programs there.
    The :description part is optional, so you could add all git-* programs from
    your $path like this:

    % zstyle ':completion:*:*:git:*' user-commands ${${(M)${(k)commands}:#git-*}/git-/}
    

    That is, it suffices to add

    zstyle ':completion:*:*:git:*' user-commands new-branch:'custom new branch function'
    

    to your zshrc.

    If you would like to handle parameters to your custom command as well, then it is a better solution to use a custom compdef file. The file referenced above has some details on that as well: Just create a standard definition file defining a git-<yourcommand> function, the default git completion will use it automatically when needed.

    Git Baby is a git and github fan, let's start git clone.