Quick Tip: Git Aware Prompt

Show your current git branch in your terminal prompt

Git Aware Prompt is a really useful shell script written by Jim Myhrberg that displays your current git branch in the terminal prompt, saving you the hassle of having to type git status every time.

Screenshot of git-aware-prompt

As you can see from the screenshot above, my current branch is displayed in parenthesis in blue, and when I change branch, the branch name in the prompt updates as well.

Install Git Aware Prompt

Create a .bash directory in the root of your user directory if one doesn't already exist, change directory and clone the project:

mkdir ~/.bash  
cd ~/.bash  
git clone git://github.com/jimeh/git-aware-prompt.git  

Now edit your .bash_profile located in the root of your user directory:

vim ~/.bash_profile  

and add the following lines and save the changes:

export GITAWAREPROMPT=~/.bash/git-aware-prompt  
source "${GITAWAREPROMPT}/main.sh"  


Now that everything is installed, all that's left to do is to configure how we want our terminal to display this information. Below is an example configuration (the configuration I used in the screenshot above), but feel free to customise this to your liking.

Add the configuration for your OS to your ~/.bash_profile:


export PS1="\u@\h \W \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "  



export PS1="\${debian_chroot:+(\$debian_chroot)}\u@\h:\w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "  


export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "  


export PS1="\[\033]0;$MSYSTEM:\w\007\033[32m\]\u@\h:\[\033[33m\w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\033[0m\]\$ "  

That's it! Restart the Terminal app if it's currently running (or source your bash_profile to load the changes), and change directory to a git initialised project to see the results.

Checkout the instructions on the Git Aware Prompt project for more configuration options.

Matt Fairbrass's Picture
Matt Fairbrass

Matt is a UX developer & designer originally from London, England now living in Sydney, Australia. Matt has over 5 years professional experience building web & mobile apps using web technologies.

Sydney, Australia
Matt Fairbrass's Picture
Write a comment Previous article » Prev »