239

E.g. on a fresh ubuntu machine, I've just run sudo apt-get git, and there's no completion when typing e.g. git check[tab].

I didn't find anything on http://git-scm.com/docs, but IIRC completion is included in the git package these days and I just need the right entry in my bashrc.

Vincent Scheib
  • 17,142
  • 9
  • 61
  • 77
  • Works for me out of the box on Ubuntu Precise (and Fedora 17). – Mechanical snail Sep 13 '12 at 03:29
  • 2
    To check whether you have it by default or not, you can run `(cd ~ && exec cat .bashrc | grep completion)`. – aderchox Dec 09 '18 at 08:23
  • 3
    Surely you could check if it's active by running `grep completion ~/.bashrc` would do the job without the `exec` and [useless use of cat](https://porkmail.org/era/unix/award#uucaletter)? – jswetzen May 27 '22 at 06:47

16 Answers16

331

On Linux

On most distributions, git completion script is installed into /etc/bash_completion.d/ (or /usr/share/bash-completion/completions/git) when you install git, no need to go to github. You just need to use it - add this line to your .bashrc:

source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git

In some versions of Ubuntu, git autocomplete may be broken by default, reinstalling by running this command should fix it:

sudo apt-get install git-core bash-completion

On Mac

You can install git completion using Homebrew or MacPorts.

Homebrew

if $BASH_VERSION > 4: brew install bash-completion@2 (updated version) Pay special care which version of bash you have as MacOS default ships with 3.2.57(1)-release.

add to .bash_profile:

  [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

For older versions of bash: brew install bash-completion

add to .bash_profile:

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

MacPorts

sudo port install git +bash_completion

then add this to your .bash_profile:

if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
fi

more info in this guide: Install Bash git completion

Note that in all cases you need to create a new shell (open a new terminal tab/window) for changes to take effect.

Sergey Evstifeev
  • 4,941
  • 2
  • 24
  • 28
  • 1
    Most *nix boxes (especially Ubuntu) already have that file, so just sourcing it to my user fixed my problem. Thanks. – nevvermind Oct 23 '13 at 14:00
  • 6
    ubuntu may have it as /etc/bash_completion.d/git-prompt – Catskul Oct 13 '14 at 17:59
  • 22
    In my Ubuntu 14.04 this file is `/usr/share/bash-completion/completions/git` . `/etc/bash_completion.d/git-prompt` is used for git prompt support, not for completion. – Rui Chen Aug 18 '15 at 05:18
  • 27
    WARNING: The Mac homebrew method only works if you have git installed through homebrew do `brew uninstall bash-completion` then `brew install git` if you had git installed through some other method before, then the above steps will work. – patapouf_ai Oct 21 '16 at 13:46
  • For mac and brew, also had to set homebrew's new bash (+4) to default shell. Required tweaking /etc/shells to add that shell as a valid shell, then chsh – Clayton Stanley Jul 07 '18 at 02:34
  • 1
    to check bash version `echo $BASH_VERSION` – Guglie May 28 '20 at 08:48
  • With Linux distros installing `bash-completion` and sourcing `/usr/share/bash-completion/bash_completion` should do. If your [distro has `/etc/bash.bashrc`](https://askubuntu.com/q/815066/43759), then sourcing is already done too. – legends2k Jul 24 '20 at 18:36
  • On homebrew, I found mine in `/usr/local/etc/bash_completion.d/` . – LexH Sep 24 '20 at 21:00
  • Also on macOS and homebrew, and you installed bash with homebrew make sure you `chsh -s /usr/local/bin/bash` rather than `chsh -s /bin/bash`. or you'll be using the system bash which requires `bash-completion` rather than `bash-completion@2` and the reported `$BASH_VERSION` will be out of sync with what the bash which is on your PATH reports with `bash --version`, causing completion to fail – ckot Jun 21 '21 at 05:11
  • For the AWS CloudShell users, the second path worked for me! – Talha Ashraf Oct 08 '21 at 11:05
  • For zsh adding this to your .zshrc should be enough: `autoload -Uz compinit && compinit` – Marcos Schroh Mar 10 '22 at 12:00
  • I found mine in `/opt/homebrew/etc/bash_completion.d`. The brew install (or brew reinstall) will tell you where it was installed, and even give you the line to add to your profile. – brianmearns Jan 19 '23 at 16:56
  • for Mac try: https://apple.stackexchange.com/a/92637/397202 – Sole Sensei Aug 08 '23 at 08:27
94

i had same issue, followed below steps:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

then add the following lines to your .bash_profile (generally under your home folder)

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

source : http://code-worrier.com/blog/autocomplete-git/

Patrick Fisher
  • 7,926
  • 5
  • 35
  • 28
jospratik
  • 1,564
  • 11
  • 19
  • I was behind a proxy so I had to set the proxy first for the curl to work `export https_proxy=proxy_ip:proxy_port` I don't really get why it does not get the settings from the system. – madlymad Jun 23 '15 at 13:57
  • 3
    it's not really needed to hide that file by a dot in front of its name. Also: careful with git versions (see the answer of wisbucky) – Walter Tross Aug 29 '18 at 10:25
  • On Linux after following the instructions above I needed to exit the terminal and open again to see the results. Also, I changed the git version in curl Url. – Waqas Mar 17 '21 at 19:31
  • for Mac: https://apple.stackexchange.com/a/92637/397202 – Sole Sensei Aug 08 '23 at 08:27
61

Most of the instructions you see will tell you to download

https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

and source that in your bash startup script like .bashrc.

But there is a problem with that, because it is referencing the master branch, which is the latest version of git-completion.bash. The problem is that sometimes it will break because it is not compatible with the version of git you've installed.

In fact, right now that will break because the master branch's git-completion.bash has new features that requires git v2.18, which none of the package managers and installers have updated to yet. You'll get an error unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config

So the safest solution is to reference the version/tag that matches the git you've installed. For example:

https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash

Note that it has a v2.17. in the URL instead of master. And then, of course, make sure to source that in the bash startup script.

wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • 9
    The macos version-specific file should already be on disk here: `/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash` Just to be safe I diffed it with the 2.17.1 version on github and it matches. – jmt Dec 15 '19 at 19:39
  • On Linux after following the instructions above I needed to exit the terminal and open again to see the results. – Waqas Mar 17 '21 at 19:32
28

Ubuntu 14.10

Install git-core and bash-completion

sudo apt-get install -y git-core bash-completion
  • For current session usage

    source /usr/share/bash-completion/completions/git
    
  • To have it always on for all sessions

    echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
    
Andrzej Rehmann
  • 12,360
  • 7
  • 39
  • 38
18

Just do this in your ~/.bashrc:

source /usr/share/bash-completion/completions/git

Other answers are telling you to install bash-completion, you don't need to do that, but if you do, then there's no need to source the completion directly. You do one or the other, not both.

A more generic solution is querying the system location as recommended by the bash-completion project:

source "$(pkg-config --variable=completionsdir bash-completion)"/git
FelipeC
  • 9,123
  • 4
  • 44
  • 38
6

on my ubuntu there is a file installed here:

source /etc/bash_completion.d/git-prompt

you can follow the links into the /usr/lib/git-core folder. You can find there an instruction, how to set up PS1 or use __git_ps1

sshow
  • 8,820
  • 4
  • 51
  • 82
kisp
  • 6,402
  • 3
  • 21
  • 19
5

macOS via Xcode Developer Tools

Of all the answers currently posted for macOS, this is only mentioned in a very brief comment by jmt...

If you already have the Xcode developer tools installed, then you shouldn't need to download anything new.

Instead, you just need to locate the already-existing git-completion.bash file and source it in your .bashrc. Check the following directories:

  • /Applications/Xcode.app/Contents/Developer/usr/share/git-core
  • /Library/Developer/CommandLineTools/usr/share/git-core

Failing that, git itself might be able to help you out. When I run git config as follows, git reports a setting which comes from a gitconfig file located in the same directory as my git-completion.bash:

$ git config --show-origin --list
...
file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig    credential.helper=osxkeychain
...

or you can always brute-force search your machine and grab some coffee:

$ find / -type f -name git-completion.bash 2>/dev/null

Thus, I have the following insertion for my ~/.bashrc:

# Git shell completion and prompt string on macOS
_git_dir="/Applications/Xcode.app/Contents/Developer/usr/share/git-core"
if [ -f "${_git_dir}/git-completion.bash" ]; then
    source "${_git_dir}/git-completion.bash"
fi
if [ -f "${_git_dir}/git-prompt.sh" ]; then
    source "${_git_dir}/git-prompt.sh"
fi
unset _git_dir

Note that this sources the git prompt-string script as well, since it resides in the same directory.

(Tested in macOS Catalina)

drmuelr
  • 955
  • 1
  • 13
  • 30
  • 2
    This also worked when I installed git for windows and then MSYS2 and I wanted to use git under MSYS2. Did `find / -type f -name git-completion.bash 2>/dev/null` in git bash to determine where the file was, then sourced it. I then grepped the entire `completion` directory for where the `__git_ps1()` function was and was able to source that in too. This allowed me to have the prompt by copying the `$PS1` environment variable over from git bash to MSYS2 bash. – Adrian Apr 29 '22 at 17:41
4

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

dorian.
  • 134
  • 1
  • 1
  • 10
3

May be helpful for someone:--

After downloading the .git-completion.bash from the following link,

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

and trying to use __git_ps1 function, I was getting error as--

 -bash: __git_ps1: command not found

Apparently we need to download scripts separately from master to make this command work, as __git_ps1 is defined in git-prompt.sh . So similar to downloading .git-completion.bash , get the git-prompt.sh:

curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git

and then add the following in your .bash_profile

source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi

source ~/.bash.git will execute the downloaded file and

export PS1='\W$(__git_ps1 "[%s]") command will append the checkout out branch name after the current working directory(if its a git repository).

So it will look like:-

dir_Name[branch_name] where dir_Name is the working directory name and branch_name will be the name of the branch you are currently working on.

Please note -- __git_ps1 is case sensitive.

Nicks
  • 16,030
  • 8
  • 58
  • 65
2

Windows

How it finally works for me on Windows 11 in the command prompt (CMD):

  • install Clink (Bash features for Windows)
  • copy git-autocomplete.lua file into C:\Users\<username>\AppData\local\clink directory
tom
  • 9,550
  • 6
  • 30
  • 49
  • The linked clink is not maintained anymore. The maintained fork is available at . PR updating the links in the repository of the completion script submitted: https://github.com/ztomm/git-autocomplete-for-windows/pull/1 – koppor Mar 16 '23 at 12:33
  • 1
    @koppor thanks. I have updated the link. Apparently, Windows no longer needs to be restarted with the new version. – tom Mar 17 '23 at 07:27
1

Ubuntu

There is a beautiful answer here. Worked for me on Ubuntu 16.04

Windows

Git Bash is the tool to allow auto-completion. Not sure if this is a part of standard distribution so you can find this link also useful. By the way, Git Bash allows to use Linux shell commands to work on windows, which is a great thing for people, who have experience in GNU/Linux environment.

Community
  • 1
  • 1
andrgolubev
  • 825
  • 6
  • 21
1

Arch Linux

Source /usr/share/git/completion/git-completion.bash in one of the bash startup files.

For example:

# ~/.bashrc

source /usr/share/git/completion/git-completion.bash

You may be able to find the script in other locations like /usr/share/bash-completion/completions/git but these scripts did not work for me.

Rob
  • 123
  • 1
  • 10
1

Mac M1

For those that are using Mac M1 environment, I was able to install via homebrew:

brew install bash-completion

Then added to my ~/.bash_profile or ~/.bashrc (whatever you use):

[[ -r "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh"

You may need to update the version number (1.3_3). You'll just need to look it up in that directory. I would love to know if there's a better way.

Matt Goo
  • 1,118
  • 1
  • 11
  • 12
  • 3
    It should be `[[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh"` - the installer suggests this. – simonhorlick Mar 08 '21 at 03:33
0

On Ubuntu 22.04 just add this line at the end of .bashrc or .zshrc

source /etc/bash_completion.d/git-prompt
tbo47
  • 2,688
  • 3
  • 20
  • 11
-1

On Github in the Git project, They provide a bash file to autocomplete git commands.

You should download it to home directory and you should force bash to run it. It is simply two steps and perfectly explained(step by step) in the following blog post.

code-worrier blog: autocomplete-git/

I have tested it on mac, it should work on other systems too. You can apply same approach to other operating systems.

clockworks
  • 3,755
  • 5
  • 37
  • 46
-1

Just put below in the .bashrc and relaunch the terminal. Navigate to Git repo to see the path in the prompt.

PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\] @ \[\033[0;36m\]\h \w\[\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\[\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] ▶\[\033[0m\] '