# Sunday Quicky #2: Git Bash Tab Completion Setup

### Introduction

Right now I’m working away on a bigger post, part of the topic being covered is the use of Git. If you don’t know what Git is follow the link, but basically it helps you to keep track of and manage changes made to files you use as part of a project. If you do any sort of coding on your machine and you’re not currently using version control, you really should consider it, it’s a game changer. There’s even a free course on Udacity to get you started, I did a previous version and it was most definitely worth it.

While re-acquainting myself with Git on the command line (most of my recent coding has been with an IDE with Git built in) I was reminded of a set of bash additions that added specific Git command tab autocompletion, this helps a lot if you remember the start of a command but not the full thing. The additions also helped you navigate around directories which are also local Git repositories by showing you information on the command prompt. So, I’m doing a quicky post on how to set that up. As far as I remember this even works with the Windows Git-bash software, and I would be very surprised if it didn’t work with the Windows Subsystem for Linux.

### Steps

If you’re new to bash, whether on Windows or Linux, then the steps to be taken may seem daunting. But you will only be downloading two new files and making changes to one existing file. If you’re nervous, before you change the existing file back it up, and if things get messed up just restore the backup and you’re back to where you started.

The Basic Steps are:

It’s that simple, even better the last step is the only one that can have unintended consequences.

So first, what files do we need. The up to date bash completion settings files are found in this Github repository, un-ironically enough. You need to download the files git-completion.bash and git-prompt.sh and save them to your home directory (/home/username/).

Now is the tricky part, which existing file to edit. There are two potential files that could be edited. These are “~/.bashrc” or “~/.bash_profile“. I’m open to correction on this one, but I believe that most modern Linux systems will have both. Although it is in relation to an OSX based question this Stack Exchange Question explains the difference. In summary if you log on to a linux machine in command line only mode the contents of .bash_profile get executed before the interface is shown, and if you use a desktop environment and open a terminal window .bashrc is executed before the terminal window is displayed. However, in my case my .bash_profile script executes my .bashrc script, so I will only have to make changes to my .bashrc script. If your .bash_profile is the same as below you’re in the same boat:

The script contents below are taken from the Udacity Git course I completed some time ago. I can’t link to it because it’s not available anymore. Full attribution goes to that course. The script contents:

# Enable tab completion
source ~/git-completion.bash

# colors!
green="$\033[0;32m$"
blue="$\033[0;34m$"
purple="$\033[0;35m$"
reset="$\033[0m$"

# Change command prompt
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff # '\W' adds the name of the current directory export PS1="$purple\u$green\$(__git_ps1)$blue \W$ \$reset"

Now if all you want is the bash tab completion you just need the first two lines (actually, just line 2, since line 1 is a comment). However the remainder helps to surface useful information on the command prompt when in and navigating around Git repositories (do you think I’ve said that enough, right so, I’ll stop).

A small change that I made to the script contents above before adding it to the .bashrc file was to change the \u to \h. This means the command prompt shows the machine’s hostname rather than my user name, which is my preference. If you look at Figures 2 and 3 below you will see the command prompt pre-changes.

Figures 4 and 5 below show the prompt post-changes, note the change in display when in a Git repository directory. More on this soon.

In Figure 6 below you’ll see that you are indeed in a Git repository. You’ll also see the various stages that the prompt goes through when you make changes to a file, add them to staging and then commit the staged changes. The “main” in brackets is the name of the current branch being worked on. It all makes working with Git on the command line so much more intuitive and these changes are highly recommended.

### Conclusion

These changes are worth it. To reiterate, these changes could break some functionality, for example in my case it breaks the whole anaconda notifier that used to show when I was in anaconda mode (don’t worry if that doesn’t make sense), so I’ll have to fix that. But again, remember you can always backup and restore if necessary.

Let me know if you have issues with this in the comments below.

Quick Edit: In relation to the Anaconda small bit of breakage, all that was need was to made sure the section of .bashrc surrounded by “# >>> conda initialize >>>” and “# <<< conda initialize <<<” came at the end of the file.