How can I set up autocompletion for Git commands?

LinuxGitBashDebian

Linux Problem Overview


I have Git (version 1.7.2.5) bash compeletion working on my Debian squeeze (6.0). Git was installed with aptitude and I am using standard debian's bash, which supports command line autocompletion.

Now, I just installed Git (1.5.6.5) on an other machine (Lenny/Debian 5.0) and the there is no autocompletion.

  1. Why is Git autocomplete not working on the second machine? How do I diagnose this?

  2. What is making completion work on my machine? I have looked for the file git-completion.bash but it doesn't seem to be on my machine. How does Git complete ever work?

  3. How can I bring git complete to the other machine?

Linux Solutions


Solution 1 - Linux

You need to source /etc/bash_completion.d/git to enable git auto-completion.

In my .bashrc it's done with:

for file in /etc/bash_completion.d/* ; do
    source "$file"
done

Solution 2 - Linux

Put the following lines in your ~/.bashrc

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

The script/program /etc/bash_completion already includes the scripts in /etc/bash_completion.d and also defines some functions needed by the included scripts.

Solution 3 - Linux

You need to install this package if missing. And then logout and login.

apt-get install bash-completion

Solution 4 - Linux

The shortest way to activate the bash auto-completion for Git on Debian is to add

source /etc/bash_completion.d/git

to the ~/.bashrc (and restart the terminal).

See also here: "Pro Git" -> 2.7 Git Basics - Tips and Tricks -> Auto-Completion.

Solution 5 - Linux

For Manjaro and other Arch-based distros. I know it's about debian, but most things are the same but sometimes not. Whatever OS you use you'll end up here.

In your ~/.bashrc add:

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

And then in terminal

$ source ~/.bashrc

Solution 6 - Linux

For Ubuntu/Debian

Install Git and bash-completion by the following command:

sudo apt-get install git bash-completion

I don't think you need to do anything else.

Solution 7 - Linux

Get the git autocompletion script:

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

Add to your .bash_profile in home directory:

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

Source your .bash_profile after this like:

. ~/.bash_profile

Solution 8 - Linux

Use Notepad++ to edit your ~/.bashrc file. Put the line at the bottom of the script with a # at the beginning of the line. Save the file. For example:

source C:\cygwin64/etc/bash_completion.d/git

Don't forget to put the entire file path after 'source' and in front of '/etc/' For example, my cygwin64 folder which contains the 'etc' folder is in my c drive so my file path is c:\cygwin64/etc therefore the line I included in my bashrc file is:

# source c:\cygwin64/etc/bash_completion.d/git

Save bashrc file. Open Cygwin Terminal ... Boom! It's go time. I then entered the following command and it worked. git clone git:\/\/github.com/magnumripper/JohnTheRipper -b bleeding-jumbo JtR-Bleeding

Solution 9 - Linux

Recent versions of Ubuntu (observed on 20.04) seem to have split completions into multiple paths. For Ubuntu 20.04, I had to add the following to my .bashrc (taken from the default bashrc found in /etc/bash.bashrc):

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

Solution 10 - Linux

At times git auto-complete disappears because you accidentally deleted your ~/.bashrc file. Check if the bashrc file is there in your home directory. If not, you can always copy it from:

/etc/skel/.bashrc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionredochkaView Question on Stackoverflow
Solution 1 - LinuxPiotr PraszmoView Answer on Stackoverflow
Solution 2 - LinuxRudgerView Answer on Stackoverflow
Solution 3 - LinuxQi LuoView Answer on Stackoverflow
Solution 4 - LinuxautomatixView Answer on Stackoverflow
Solution 5 - Linuxlava-lavaView Answer on Stackoverflow
Solution 6 - Linuxnkalra0123View Answer on Stackoverflow
Solution 7 - LinuxNoorView Answer on Stackoverflow
Solution 8 - LinuxFuzzyBirdView Answer on Stackoverflow
Solution 9 - LinuxcqcallawView Answer on Stackoverflow
Solution 10 - LinuxhlosukwakhaView Answer on Stackoverflow