GIT autocompletion and enhanced bash prompt
10 Comments Published May 23rd, 2010 in Apple, Git, Linux Tags: Bash, Git, How-To, Linux, OSX, Tools, Tricks.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:
![]()
"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
fi
if [ -f /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh ]; then
. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
fi
GIT_PS1_SHOWDIRTYSTATE=true
if [ -f /opt/local/etc/bash_completion ]; then
. /opt/local/etc/bash_completion
fi
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:
Branch:

Status:

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 Pingback on Aug 17th, 2010 at 23:34
- 2 Pingback on Mar 11th, 2011 at 21:04
- 3 Pingback on Sep 2nd, 2011 at 12:43
Leave a Reply
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Apr | Jun » | |||||
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 | ||||||
Follow me
Archives
Categories
- Android (3)
- Apple (29)
- Books (7)
- Eclipse (14)
- Errors (5)
- Firefox (7)
- Git (3)
- Hardware (18)
- Horror Code (8)
- Internet (21)
- Java (104)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (26)
- Linux (52)
- Opinions (26)
- OSX (11)
- OWNER API (2)
- Python (1)
- Software (33)
- Speeches and Conferences (8)
- Unix (5)
- Web (23)
- Windows (19)
Tag Cloud
Android apple architecture Bash configuration CSS Development Düsseldorf Eclipse Git Google Hardware hdr How-To howto Java JAXB job Karmic Linux lion MacBook music Open Source Opinion OSX os x patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu unix video Web Workaround XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links


















litterally a fantastic post. this was so needed.
Awesome, especially the indication of the (active) branch in your command prompt. Should be on every git user’s bashrc profile. :-)
this rocks!
thanks :)
Thank you for this.
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
GIT_PS1_SHOWDIRTYSTATE=true
PS1=’\[\e[0;32m\]\u@\h\
\[\e[0;32m\] \w\
\[\e[0;33m\]$(__git_ps1)\n\
\[\e[0;37m\]\$\
\[\e[0m\] ‘
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
GIT_PS1_SHOWDIRTYSTATE=true
PS1="\[${green}\]\u@\h\
\[${yellow}\] \w\
\[${yellow}\]$(__git_ps1)\n\
\[${white}\]\$\
\[${color_off}\] “
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
GIT_PS1_SHOWDIRTYSTATE=true
PS1="\[${green}\]\u@\h\
\[${yellow}\] \w\
\[${yellow}\]"'$(__git_ps1)'"\n\
\[${white}\]\$\
\[${color_off}\] "