How can I set up autocompletion for Git commands?
Asked Answered
H

10

60

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?

Harper answered 23/6, 2012 at 22:21 Comment(8)
This may be more suitable question for either serverfault.com or unix.stackexchange.comArchaize
Please make sure that you compiled git with auto-completion if you compiled from source. Also make sure that you are using the same shell as on your old machine and that it supports auto-completion. As you can see, I'm not sure what is causing it so leaving it as a comment. It's probably either your shell not providing completion for it or git not installing completion properly. Good luck.Ellery
@Archaize thanks. I will post again on unix.stackexchange.comHarper
@MateuszKowalczyk thanks. I installed git with aptitude. I'm using standard debian's bash which supports completion. Both machines have the same Debian version.Harper
Do you have bash-completion installed? How about git-completion (although this should come in with git by default). Good luck on stackexchangeEllery
Try source /etc/bash_completion.d/git it should enable it in the current shell. If it does, check your bash init scripts.Hadwyn
@Banthar Thanks this makes Autocomplete working.Harper
superuser.com/questions/1310317/…Glaswegian
H
65

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
Hadwyn answered 23/6, 2012 at 22:41 Comment(2)
This did not work for me, since some of the completion scripts inside /etc/bash_completion.d/ used helper functions defined in bash_completion.Swaney
This works for CentOS 7 as there is no /etc/bash_completion file.Calefactory
A
65

For Debian Squeeze (6.x):

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.


For Ubuntu Bionic and up (^18.04):

Put the following lines in your ~/.bashrc

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

For Fedora 20 & macOS High Sierra

See comments

Allonym answered 1/7, 2012 at 15:38 Comment(5)
In my Fedora 20 system bash_completion is located in /usr/share/bash-completion/. Running updatedb && locate bash_completion should help you find it.Swaney
This also solved my "no command 'have' found" issue. The "have" function is defined in /etc/bash_completion, and many scripts in /etc/bash_completion.d/ depend on it.Eugenol
i know it's not a mac question, but I'd note I don't think this file (/etc/bash_completion) exists high sierra. Also, my bash_completion.d is at /usr/local/etc/bash_completion.d. @Piotr's answer with the above directory the trick though.Vegetarianism
/etc/bash_completion.d is a legacy directory, where bash completions were loaded eagerly to your shell, whereas /usr/share/bash-completion/completions/ is a new location for completion files, and completion files in this directory loaded on-demand, i.e. only if they needed, check docAugustus
On Rasperry Pi OS (Debian-based), remember the bashrc file is located under /etc/bash.bashrcAlas
A
44

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

apt-get install bash-completion
Allopath answered 10/11, 2016 at 19:55 Comment(4)
after installation execute source /etc/bash_completionWollis
worked on Arch Linux. I had to restart my terminals after the installation.Giacobo
It also worked on Android Termux.Neace
Works for distrobox emulation of Ubuntu 22.04 too!Schmitt
M
15

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.

Massingill answered 31/1, 2013 at 20:56 Comment(0)
B
5

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
Behan answered 19/10, 2021 at 7:53 Comment(0)
B
3

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
Bis answered 27/11, 2019 at 12:59 Comment(1)
Finally a solution that works for Arch-based distros (which are becoming increasingly more popular these days). Cheers! \oPullman
V
3

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.

Vivien answered 25/12, 2019 at 17:31 Comment(2)
Make sure you try to autocomplete a common git command like "git clone" because it's just that SOME commands don't autocomplete like "git update-index"Glaswegian
Also works for some RPM-based distributions, with yum install bash-completionDelegate
M
2

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
Myrica answered 20/11, 2020 at 19:49 Comment(0)
M
1

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

Meneau answered 21/5, 2016 at 8:49 Comment(1)
the question is about debian, not windowsHonebein
C
0

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
Coltish answered 9/5, 2017 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.