Back to Blog

Get in Touch

Git Status in Your Prompt

By Brent Collier February 2, 2009 in tips, git, terminal

Missing

I've been using Git for almost a year now, but I didn't really start using Git until recently when I began working for Intridea.  Once I started using Git on a daily basis, dealing with multiple projects, and multiple branches per project, I would occasionally make the mistake of changing code on the wrong branch.  While annoying, it was easily fixed by stashing the changes and applying the stash to the proper branch.

As much as I love git stash, this began to get old, and constantly hitting up git status to check the current branch wasn't cutting it.  After a bit of googling, I found this.  It describes how to add the current branch name and its clean/dirty status to you terminal prompt.

Just add this to your .bash_profile: 

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='\u:\[\033[31;40m\]\w\[\033[0;33m\]$(parse_git_branch)\[\e[0m\]$ '

And you should end up with something that looks like this:

My terminal prompt

As you can see, I like to use a bit of color to help things stand out.

So far this has been immensely helpful.  With this info at a glance, I always know where I am and how I last left things.

Tags:

tips git terminal
Medium

Brent Collier

Brent spent several years working on J2EE projects doing mostly server-side development for large clients in the financial sector. After leaving the enterprise world for a mobile startup, a coworker turned him on to Ruby on Rails. With this newfound interest, Brent went on to co-found and develop Yappd, a microblogging social network built on Rails. He has since continued to hone his Rails skills, concentrating on social content and collaboration. As a Senior Engineer, Brent employs agile methodologies whenever possible and strives to produce simple and effective solutions.

More posts by Brent Collier

Brent Collier

I tell myself that I write JS every day, but I don’t. I write jQuery. Don...

Brent Collier

As a software engineer, I'm like an over-caffeinated monkey for two reason...

Brent Collier

I recently spent a day hanging out with a few of the guys at Viget Labs hacki...