Rails Dog Blog

Current Articles | RSS Feed RSS Feed

Custom Bash Prompt for Git Branches


example of git bash prompt

If you are an active git user then you may find yourself constantly switching between different branches in the same project as you are working. If you're not doing this, you're probably not experiencing the full power of git. I recently added some functionality to my bash prompt that shows the current git branch in green and a yellow asterisk if there are uncommitted changes in the project.

Here's how to do it. First you will need a copy of .git_completion.sh in your home directory. This script has some functions that we will use to customize the bash prompt. It also has some nice side effects like tab completion for branch names, etc. Now simply add the following to your .bash_profile

 source ~/.git_completion.sh 

function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"

export PS1='\h:\W$(__git_ps1 "[\[\e[0;32m\]%s\[\e[0m\]\[\e[0;33m\]$(parse_git_dirty)\[\e[0m\]]")$ '


Currently, there are no comments. Be the first to post one!
Post Comment
Website (optional)

Allowed tags: <a> link, <b> bold, <i> italics