How to customize terminal bash prompt: add current Git branch and state

Hey, Terminal pro. Time to make your terminal work easier and more efficient by customizing your terminal prompt to alert you to your current git state and branch.

A majority of the time my work with terminal is to do with Git. But it’s easy to get lost even if you have a really easy git workflow in place. Have you ever made commits to your master branch when you thought you were working in develop? Annoying! Here’s a tip that will help you keep your work with git in order. We’ll customize your terminal prompt so that it will always let you know which branch you’re working in. And if you’re working in a dirty or a clean state. It’s pretty awesome and has kept me from commit headaches countless times.

It can look something like this:

Screenshot of git status

What the command line looks like in various git states.

Edit your .bash_profile

Open terminal and go to your user’s directory.
cd /Users/Yourname

Look for .bash_profile. This is where we’ll be putting the snippet.
ls -al (don’t forget the -a since .bash_profile is a hidden file)

Screenshot of files list in terminal

Using the -a and -l flags to list hidden files in vertical list

Open .bash_profile if it exists. If not, create it.
To open
nano .bash_profile

To create
touch .bash_profile

Then just paste in the following
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1=’$(__git_ps1 “(%s)”)’

When you’re done you’ll see alerts like the following:

(branch *) – when you have changes that need to be added and committed

(branch +*) – some changed files have been added and are ready to be committed, some have not yet been added

(branch +) – files have been added and need to be committed

After that there are endless ways to customize your command line. Here’s what my command line prompt looks like. I’ve added some color and changed how my current location is displayed (code for that below image).

You can change colors and lots of other stuff.

You can change colors and lots of other stuff.

Copy mine:

export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\n\[\033[0;35m\]\w$(__git_ps1 " \[\033[0;31m\](%s)") \n\[\033[0;32m\]\$\$\$ '

Mess around if you like. I’ll warn you that things can get messy and confusing fast. The styling commands for colors are not like anything I’ve used in my web dev work. Check out wiki.archlinux.org for some more tips.

Leave a Reply

Your email address will not be published. Required fields are marked *