Bash completion for Git is a very handy utility for a Git and Bash user. For the past few months, I have been trying out Git for source control. Having Bash completion for Git was useful in my attempts to get acquainted with the commands. So what exactly is Bash completion for Git?

If you are on Mac OS X, Bash comes with command completion. After typing part of a filename, command or pathname in Bash, hitting the <TAB> key will trigger Bash to prompt you with either the remaining portion of the filename or path. A subsequent <TAB> will then trigger a list of filenames or path that matches what has been keyed in so far.

Bash completion for Git works the same way by prompting you with available commands or filename.

One possible installation method would be using MacPorts. MacPorts is a really useful tool for compiling, installing and upgrading open-source software on the Mac OS X. It is command-line driven and is relatively simple to use.

sudo port install -u git-core +bash_completion +doc +svn

The command above installs Git with variants (variants are essentially additional options). In this case, MacPorts is installing Git with SVN, Git Man files (documentatons) and the main subject of our interest: Bash completion itself.

When installing new version of a port, MacPorts will make the existing version inactive and then install the latest version. Over time, you can wind up with a lot of inactive version for a port. The -u flag simply instructs MacPorts to uninstall any inactive ports.

The last step to this is to edit the user Bash configuration file in your home folder (~/.bash_profile) or the system-wide one (/etc/profile) and insert in the following:

if [ -f /opt/local/etc/bash_completion ]; then
  . /opt/local/etc/bash_completion
  export PS1='\h:\W$(__git_ps1 "(%s)") \u\$ '
fi

Apart from checking if the bash completion script exists and running it, the code above will also modify the Bash prompt so that you can always tell which Git branch you are currently working on. Handy information to have at the Bash prompt.

And it is done, Git for source control with extra help from Bash completion.

What others think

andhapp

andhapp
Jan 16 2009

Thanks for this and you have a very nice site layout. Impressive...

BigEvilEmpire

BigEvilEmpire
Jan 16 2009

Hope it was useful information and thanks!