Skip to content
Felipe Contreras edited this page Jan 28, 2022 · 12 revisions

Installation

Simply do make install, or manually copy the files:

cp git-completion.zsh ~/.local/share/git-completion/zsh/_git
cp git-completion.bash ~/.local/share/bash-completion/completions/git
cp git-prompt.sh ~/.local/share/git-completion/prompt.sh

Then edit ~/.zshrc and add the following:

fpath=(~/.local/share/git-completion/zsh $fpath)

source ~/.local/share/git-completion/prompt.sh

You can use any location you want, if you already have a ~/.zsh directory you can place the script there.

zinit

If you are using zinit, git-completion can be loaded as a plugin easily:

zinit light felipec/git-completion

Oh My Zsh

git-completion is already bundled in oh-my-zsh as the gitfast plugin, so just enable it:

plugins=(... gitfast)

Completion configuration

GIT_COMPLETION_CHECKOUT_NO_GUESS=1
GIT_COMPLETION_SHOW_ALL=1
GIT_COMPLETION_CHECKOUT_NO_GUESS

Do not include "DWIM" suggestions in git-checkout and git-switch completion.

GIT_COMPLETION_SHOW_ALL

Suggest all options, including options which are typically hidden.

Prompt configuration

You need to add $(__git_ps1) to your PS1, for example in ~/.zshrc:

setopt PROMPT_SUBST
PS1='%m %~$(__git_ps1 "[%s]") %# '

If you want colors, simply add:

GIT_PS1_SHOWCOLORHINTS=1

For more options see Prompt.

Aliases

By default aliases and custom commands are automatically handled, but you might have custom functions, for example:

glm() { git log ^master ${@:-HEAD} ; }

There’s no way for the completion to do the sensible thing here, unless you manually configure it.

You can use Zsh’s compdef:

compdef _git glm=git_log

Complex aliases

Git has the option to specify complex aliases, for example:

git config --global alias.func '!f() { function $@ ; }; f'

It is possible to specify the completion you want for such alias:

git config --global alias.func '!f() { : git log ; function $@ ; }; f'
Clone this wiki locally