Setting up Git for Ubuntu with autocompletion

If you ever used any Linux distribution, you are most likely familiar with the default Bash completion functionality. You start typing a command, hit Tab, and the command gets automatically completed. If there are multiple commands that match what you typed so far, you can hit Tab two times, and Bash displays a list of all possible completions. The same works for variables and filenames, but not for the many commands Git provides.


The good news is that we can speed up Git with autocomplete pretty easily. And as you probably want to do this as soon as you installed git, let’s just go through the installation and basic setup of Git too.

Installing and setting up Git

Really simple on Ubuntu, just copy this command to your terminal:

sudo apt-get install git

Now let’s set our name and email address and let’s also enable color mode with these commands:

git config --global user.name "John Doe"
git config --global user.email "john.doe@nobody.org"
git config --global color.ui true

That’s it, you’re done.

You can check the version of Git with this command:

git --version

… and you can view your setting with this one:

git config --list

Setting up Git autocomplete

You can download the bash extension script from the main repository of Git from the following URL: https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

You can either download it and save it to your home folder or you can just execute this command in the terminal without ever visiting the link above:

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

No matter how you acquired the script, you need to rename the file to start with a period. You can do this in the terminal with this command:

mv git-completion.bash .git-completion.bash

Now that we have the file in your home directory with the correct filename, you need to edit either the .bash_profile or .bashrc file found also in your home directory to use the extension. Let’s use the Nano file editor to make the changes assuming you have a .bash_profile file there.

nano .bash_profile

In the editor window type in this code at the end of the file:

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

Press Ctrl + x to exit the editor saving the changes and press Enter to overwrite the file being edited.

For the effects to take place you need to restart bash, so close the terminal and open it again. Test the autocompletion by typing “git h” and press Tab. “git h” should now be autocompleted to “git help” if you did everything right.

Related Posts

Sorry, no similar posts found.