There are a couple of cool features in GIT that may not be well known by everyone, depending on which platform you are using. For example on Linux it is quite common to have the bash completion working, while in Windows msysgit comes with a cool prompt which displays the current branch and some status information on the command line, as well as with the bash completion.
Git (version 1.7.0, and probably 1.7.1) on OS X comes with nothing, so here I show how to enable the command line completion and the git enhanced prompt. You can use the same tip on Linux to get the GIT enhanced prompt.

Here you can see how my prompt looks like:
Git Prompt and Completition

"luigi@hal9000:~/bin" is my usual prompt (username@hostname:folder), the red part indicates current branch, and the asterisk means that I have some modified files to commit.
In the above sample, also you see what happens when I type "git cl[tab-key]". The tab key triggers command line completion, and I can see available git commands starting with "cl"; in this case clone and clean.
The completion works not only on the command names, but also on file names and git object names, for example on branch names: if you type "git checkout mas[tab-key]" you should get it expaended to "git checkout master".

To enable the GIT bash completion, on OSX, you need to execute the script located at /usr/local/git/contrib/completion/git-completion.bash. This script is installed together with GIT utilities on OSX. To have its features enabled when you open a terminal session you should add following lines to your .profile:

# Set git autocompletion and PS1 integration
if [ -f /usr/local/git/contrib/completion/git-completion.bash ]; then
  . /usr/local/git/contrib/completion/git-completion.bash
if [ -f /opt/local/share/doc/git-core/contrib/completion/ ]; then
    . /opt/local/share/doc/git-core/contrib/completion/

if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion

PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

The GIT_PS1_SHOWDIRTYSTATE variable is used to indicate that the prompt should display the asterisk or other characters to indicate that the state of the working tree is "dirty" (something is changed and it has not yet been committed).
The script git-completion.bash enables bash completion and contains also some functions that can be executed when the prompt (defined in the PS1 variable) is shown. In this case the function displayed is named __git_ps1 and it displayes the current branch; the rest of the PS1 text is to display a nice prompt with colors.
For Linux user (at least for Ubuntu ones), it is just necessary to define the PS1 variable in the ~/.bashrc file, as the git completion script is usually included by default when installing git.

Another nice trick, that is not enabled by default, is the coloring for the following git commands: branch, status and diff. Having colors enabled helps to faster and easier understand the output. To enable colors issue the following commands:

$ git config --global color.branch auto
$ git config --global color.diff auto
$ git config --global color.status auto

Configuring above git properties will affect color display as in following examples:

git branch


git diff

Hope you find those tricks as useful as I do find them.

I also suggest you this article:
Must Have Git Aliases: Advanced Examples

10 Responses to “GIT autocompletion and enhanced bash prompt”  

  1. 1 Rodney

    litterally a fantastic post. this was so needed.

  2. 2 Mattias Geniar

    Awesome, especially the indication of the (active) branch in your command prompt. Should be on every git user’s bashrc profile. :-)

  3. 3 cvasilak

    this rocks!

    thanks :)

  4. 4 Antonio

    Thank you for this.

  5. 5 Per

    Here’s a PS1 def giving being very close to the windows git bash default (including newline before the $).
    Just paste it at the end of ~/.bashrc

    \[\e[0;32m\] \w\
    \[\e[0m\] ‘

  6. 6 Per

    Here is the same with variables for the colors for being a bit more readable.

    green=’\e[0;32m’ # Green
    yellow=’\e[0;33m’ # Yellow
    white=’\e[0;37m’ # White
    color_off=’\e[0m’ # Color Reset

    \[${yellow}\] \w\
    \[${color_off}\] “

  7. 7 Per

    Sorry for spamming. The previous version was not all correct. Mod may delete my two previous posts if s/he wants.

    So this is the code for making the terminal look very similar to msysgit:

    green='\e[0;32m' # Green
    yellow='\e[0;33m' # Yellow
    white='\e[0;37m' # White
    color_off='\e[0m' # Color Reset

    \[${yellow}\] \w\
    \[${color_off}\] "

  1. 1 Bits And Pix » OSX: Add autocomplete for git
  2. 2 Getting started with Git: linkpost ~ Mattias Geniar
  3. 3 Git prompt and autocompletion – El emprendedor curioso

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


May 2010
« Apr   Jun »

Follow me

twitter flickr LinkedIn feed

Subscribe by email

Enter your email address:



Tag Cloud