It seems many are coming here to figure out how to enable tab completion on their OS. It's important to note from the beginning that git tab completion is shell-dependent, not OS-dependent. These days, most shells work across a variety of Unix-based operating systems, like bash, zsh, or fish. A select few work across all major OS's, like Powershell.
If you're not sure which Unix shell you're using, try echo $SHELL
or see this question.
Bash (most Linux distros)
On most Unix distributions, git tab completion is meant to work out of the box, installed to /etc/bash_completion.d/
or /usr/share/bash-completion/completions/git
. You're likely here because it isn't working, which may be because the completion script isn't getting used. Try adding this line to your .bashrc
:
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git
Then try tab completion again in a new terminal.
If git autocomplete is broken or not installed, reinstalling by running this command should fix it:
sudo apt-get install git-core bash-completion
Once again, try tab completion again in a new terminal.
For more details, see the git book Appendix A: Git in Other Environments - Git in Bash
Installing git tab completion on macOS for bash users
If you use the bash shell in macOS, you can install git completion using Homebrew or MacPorts. See @sergey-evstifeev's answer for how to do this.
Zsh (macOS, some Linux distros)
MacOS and several Linux distros use Zsh as the default terminal shell. To enable git tab completion in Zsh, add to your .zshrc
:
autoload -Uz compinit && compinit
Then try tab completion again in a new terminal.
Optional: Consider also adding the Oh My Zsh plugin for themes with git info.
For more details, see the git book Appendix A: Git in Other Environments - Git in Zsh
Powershell (Windows)
Tab completion on Windows can be added to Powershell with posh-git. (Note that this is not supported in Command Prompt).
To install posh-git in Powershell,
script execution policy must be set to either RemoteSigned
or Unrestricted
. Check the script execution policy setting by executing Get-ExecutionPolicy
. If the policy is not set to one of the two required values, run PowerShell as Administrator and execute Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
.
Then install with PowerShellGet:
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force
Then try tab completion again in a new terminal.
For Chocolatey, Scoop, or manual installation instructions, see the posh-git GitHub page.
Optional: Also consider upgrading to Powershell 7, which has command-line IntelliSense for intelligent tab completion.
For more details, see the git book Appendix A: Git in Other Environments - Git in Powershell