Services Spotlight:

Enhance Your Git Prompt

Written By Corlew Solutions
Updated September 2, 2016
Published April 10, 2016
Why Corlew Solutions?

We write great software and provide amazing technical support. Contact us today if you need help with a software project:

  • Website: Send an inquiry
  • Email:
  • Phone: (703) 688-3058

Article Technology Info

This article discusses the following technologies:

  • Fedora - Fedora is an operating system based on the Linux kernel, developed by the Fedora Project and sponsored by Red Hat.
    (website | download | wiki)
  • Git - A free and open source distributed version control system
    (website | download | docs)
  • Ubuntu - A Debian-based Linux operating system that is a popular server choice.
    (website | download | wiki)

Bash Completion in Action

Tired of typing git staus to see the state of your git repo? The main git repo on GitHub provides a script we can use to enhance our bash prompt with state information about a repo when we cd into a directory Git is watching. The script works by displaying the currently checked out branch name, along with some optional symbols that inform you of the repo’s state. The (master+%=) in yellow, in the above picture, shows the script in action. The rest of this article explains how to get this working in your bash terminal and hopefully, once installed, you’ll find yourself typing git status way less.

Start by downloading the script; the instructions below save it to ~/bash_scripts/git-prompt.sh:

1
2
3
4
mkdir ~/.bash_scripts
cd ~/.bash_scripts
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -o git-prompt.sh
chmod +x ~/.bash_scripts/git-prompt.sh # make sure the script is executeable

The comments at the front of the script explain the installation process and all the configuration options. You can open the script locally or view it on GitHub to view the full installation and configuration instructions. We’ll cover the installation process and the most common configuration options below.

We only need to edit a single file, ~/.bashrc, to complete the installation and configuration. We’ll do this in stages so keep the file open until the end. First add the following to the end of ~/.bashrc:

~/.bashrc
1
source ~/.bash_scripts/git-prompt.sh

This tells bash to load the script when you login. At this point, the script will load, but it will only show the currently checked out branch on the prompt (well almost, we still have to configure the prompt itself). If we want all the fancy status symbols, we need to tell the script what we would like shown. To do that, we set some environment variables in ~/.bashrc by adding a couple more lines. Here are the settings we use to get your started (we’ll explain what they mean soon):

~/.bashrc
1
2
3
4
# configure the git-prompt.sh script
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1

The 1’s in the above file actually aren’t special. We just need to set them to a non empty value for the script to understand that we want it to show information. With the above export lines in place, the script is now configured to output status info. It will do this by adding *,+,$,%, etc… to our prompt. The following table summarizes what each of the most common symbols mean. For instance, because we set GIT_PS1_SHOWDIRTYSTATE=1, the script will put a * when we have unstaged changes in the repo, and a + when we have staged changes.

Variable Symbol Meaning
GIT_PS1_SHOWDIRTYSTATE=1 * Some file has changed, but everything is unstaged
GIT_PS1_SHOWDIRTYSTATE=1 + A tracked file has changed and staged (i.e. something is ready to commit)
GIT_PS1_SHOWUNTRACKEDFILES=1 % An untracked files exist
GIT_PS1_SHOWSTASHSTATE=1 $ Something is stashed
GIT_PS1_SHOWUPSTREAM=“auto” < Local HEAD is behind upstream
GIT_PS1_SHOWUPSTREAM=“auto” > Local HEAD is ahead of upstream
GIT_PS1_SHOWUPSTREAM=“auto” <> Local HEAD has diverged from upstream
GIT_PS1_SHOWUPSTREAM=“auto” = Local HEAD is same as upstream

Again, we encourage you to read the comments in the script; there are more options available then what is shown in the table. With our environment variables in place, the final step is to actually add the information to the prompt. Add the following to ~/.bashrc after the export lines:

~/.bashrc
1
export PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \W\[\033[01;33m\]$(__git_ps1)\[\033[01;34m\] \$\[\033[00m\] '

This line tells bash how to display our prompt. The important part is the $(__git_ps1) which is the point where the script injects it’s information. The other messy looking code is bash’s way of setting escape sequences for color and info on the prompt. You can read more about customizing the prompt here if you want to pick different colors and information.

At this point, everything is done. You simply need to save and close the file, then execute the following command on the terminal to reload your .bashrc file with the changes we just made. If everything worked, you can navigate to a git enabled directory and see the status.

1
source ~/.bashrc

We hope you found this article useful. If you see any mistakes, missing features or ways to improve it, please let us know in the comments below so we can update its contents. If you're willing to link to us, we would sincerely appreciate it!

References

Corlew Solutions is a Web Design and Web Application Development company based in Fairfax Virginia, minutes away Washington D.C. If you're looking for great web design from the Northern Virginia area or web design from the Washington D.C. area we can help. Contact Us today!

comments powered by Disqus