Sunday Quicky #2: Git Bash Tab Completion Setup


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.


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:
  1. Download necessary files and store them in your Home directory
  2. Change your bash settings file to refer to the downloaded files so that your prompt reflects when your in a Git repository directory

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 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:

Figure 1: Contents of .bash_profile. Simply checks that .bashrc is present and executes it.

The next thing to do is to make the changes required to get the bash completion of Git commands to work. The changes will also add some information to the command prompt that lets you know if you are in a Git repository directory and if there are uncommitted changes to any of the files, it really is very useful. If your .bash_profile looks like mine, check your .bashrc file. If that has very little in the way of contents you could just copy and paste the script contents below into your .bashrc file and restart your terminal and you should be good to go. However if, like me, you have had numerous packages make changes to your .bashrc then the following script contents should be added at the end of your .bashrc. Again, before you make changes either backup your .bashrc or be sure you know how to undo your changes, just in case. This won’t mess up your install, but it could cause your terminal to be a bit less useful…

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!

# Change command prompt
source ~/
# '\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.

Figure 2: Home directory bash prompt pre bash prompt and tab completion settings
Figure 3: Directory with Git repository pre bash prompt and tab completion settings

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.

Figure 4: Home directory bash prompt post bash prompt and tab completion settings
Figure 5: Directory with Git repository post bash prompt and tab completion settings

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.

Figure 6: Proof we’re in a Git repository and prompt changes at various interactions


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.

Leave a Reply

Your email address will not be published. Required fields are marked *