User login

Enhance your prompt with git information

Git provides a series of enhancements for the bash prompt. It can display the current branch when inside a working copy. Here's an example from my machine:

stefan@debian:~/Documents/Projects/Drupal/test(master *)$

It shows the branch name and indicates by an asterisk that I have uncommitted changes.

On Debian based systems you can enable that feature in your .bashrc if you are using bash as your shell. It requires the package bash-completion which can be installed via apt-get install bash-completion. On my system (Debian squeeze) I have a .bashrc file containing two prompt definitions (the lines starting with PS1). Whatever prompt you choose in your setup adding $(__git_ps1 "(%s)") adds the branch info to the path if available:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}[\033[01;32m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[31m]$(__git_ps1 "(%s)")[\033[01;34m][\033[00m]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1 "(%s)")\$ '
fi

Not that you can modify the appearance of the git prompt. If you like to have curly braces around the branch name just replace the parentheses inside the quotes around %s.

There are some variables that enable additional features. In order to have bash indicate the state of your working copy add GIT_PS1_SHOWDIRTYSTATE=1 somewhere in .bashrc. Read /etc/bash_completion.d/git for more.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.