Adding Git Data to Your Bash Prompt

by Buddy Lindsey on May 14, 2013

Git Logo

A well setup bash prompt can save you a lot of time, and be amazingly useful giving you all the information you need, quickly. Unfortunately, you need to set it up properly which is a bit tricky. We will walk you through setting up your prompt with git data.

Bash 4+

First thing you need to make sure of is you have bash 4.0 or higher.

Linux

This should be default.

Mac OS X

Mac OSX is a bit more complicated, but not by much, please follow a previous blog post: Upgrade Bash to 4+ on OS X.

Download git-prompt and Activate

Next you will need the bash functions for your environment to use. Easiest thing to do is download the file and source it in your `~/.bash_profile` or `~/.bashrc` file.

Download git-prompt.sh

Similar to setting up git-autocomplete you need to download the git-prompt.sh file, and source it in your `~/.bashrc` or `~/.bash_profile`.

cd ~/
wget https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh
mv git-prompt.sh .git-prompt.sh

Activate

Then you need to source the file by adding the following to your `~/.bashrc` or `~/.bash_profile`:

source ~/.git-prompt.sh

Adding Git Data to Your Prompt

One of the tricks to setting your prompt is setting the PS1 environment variable, will cover it in more detail in another post. Since you will be setting the PS1 in your `~/.bashrc` or `~/.bash_profile` you can actually do some fun things with bash scripting to have some very dynamic prompts. Most noticeably the __git_ps1() function was designed to get the current branch, plus more information, and bring back that data for your bash prompt.

The easiest thing to do is add the following line to your `~/.bashrc` or `~/.bash_profile`:

export PS1='$(__git_ps1 "(%s)") \W $'

This will produce a prompt similar to:

(master) Programming $ 

What you are seeing is the __git_ps1 function adds the (master) segment to the prompt if you are in the folder of a git repository. The \W shows only the current folder you are in, not the path. This is convenient when you are super deep in folders.

What About Staged and Non-Staged Changes?

One of the good things about this is you can easily tell if you have staged and non-staged changes. In order to use this feature you need to add the following line to your `~/.bash_profile` or `~/.bashrc` file.

GIT_PS1_SHOWDIRTYSTATE=1

This is a flag for your prompt to show a * and + after the branch name. * means unstaged changes and + is for staged changes. Having neither means you have no uncommitted changes.

Conclusion

Adding this to your prompt is extremely useful to have. There have been a number of times I have forgotten which branch I am in and push the wrong one to a remote. I have also tried to rebase and not realized I have uncommitted changes. Now my prompt easily ‘prompts’ me to know what I am doing. This saves me time everyday.

Related Posts:

{ 0 comments }

Quick Intro to Python Requests Library

by Buddy Lindsey on May 7, 2013

Calling 3rd party services is an essential part of web development these days. I did a quick little python article on Basic urllib GET and POST With and Without Data. It was a good look into how python natively handles doing GET and POST HTTP actions. However, there is a better way, and that is with the requests library.

GET

Most of what you will be doing is using different HTTP Verbs. GET is probably the one you will use the most, and it is simple to do. Look at the following code example:

import requests
r = requests.get('https://localhost/user/buddylindsey/')
print(r.text)
print(r.json())

You can even pass in some data with your get request.

import requests
r = requests.get(
        'https://localhost/user/', 
        params={'user':'buddy'}
    )

This builds up the request and adds the data as a query-string auto-magically. Makes things a bit easier don’t you think?

POST

import requests
r = request.post(
        'http://localhost/user/new', 
        {'username':'buddylindsey', 'password':'password'}
    )
print(r.status_code)
print(r.json())

Other Verbs

OPTIONS, HEAD, PUT, PATCH and DELETE are also available. To be honest I haven’t used OPTIONS, PATCH, or HEAD before so while I have a basic understanding of how they work I won’t attempt to explain it to you. Instead please visit the requests HTTP verbs docs to get a better understanding of their usage.

More on Response Object

The great thing about the response object you get back after making a request is you all of the options available for things to do and know. Here are some of the methods and properties you have access to:

  • headers
  • status_code
  • text
  • json()
  • encoding

There are many more, but these are probably the ones you will use the most.

Conclusion

The requests library is one of the best, if not the best, libraries for calling 3rd party web services and acting upon them. This is a powerful tool to have in your toolbelt, it is recommended to learn it, and learn it well.

Related Posts:

{ 0 comments }

Upgrade Bash to 4+ on OS X

April 30, 2013

Unfortunately, Apple has decided to ship an old version of bash shell. When I go back and forth between linux and OSX sometimes I hit minor inconsistencies because of this. One big one is the git-prompt scripts. As such I finally decided to upgrade to version 4 of bash. It is a very easy process, [...]

Read the full article →

Adding Git Autocomplete to Bash on OS X

April 23, 2013

If you use git on the command line a lot then git bash autocomplete is an amazing tool to have in your toolbelt. Unfortunately, just by installing git you don’t get to use it. You need to add command line functionality to your environment. The easiest way to add autocomplete is by downloading a file [...]

Read the full article →

Jenkins and Github Pull Requests

April 16, 2013

One of the things people love about travis-ci is it will build pull requests, but most people don’t realize Jenkins can do pull requests as well. It is also very simple to configure Jenkins to do pull requests using the correct plugin. Install the Plugin Install this plugin through the manage Jenkins admin section. Github [...]

Read the full article →

Python Date and Datetime Objects – Getting to Know Them

April 9, 2013

Properly dealing with dates can be hard, but they don’t have to be as long as you understand the basics. I have had to deal a lot with dates lately with a few Django applications, and life got a whole lot easier once I figured out the basics with python. In this post we are [...]

Read the full article →

Minimum Grep You Need to Know

April 2, 2013

You need to find a phrase in over 200 files worth of code. Manual searching is not a feasible option. If you are like me you know about grep, but it has always made you nervous. It is so powerful reading the man page was like a tech manual for an engine. Fortunately, getting the [...]

Read the full article →

Interview With Founder of Picirus

November 21, 2012

I got to sit down, shortly, with Tim Morgan the founder of Picirus a backup solution where you backup your cloud data, not to the cloud. This is a fun device that was created during, and since, Startup Weekend Tulsa has a very bright future ahead of it. So without further ado here is the [...]

Read the full article →

Working With Unix Processes – Book Reivew

October 29, 2012

This book alone proves why books are still relevant. It is a fairly short read, but the quality of the content is one of the best, with regards to a technology book. The material covered is very important if you want to do anything beyond basic websites, which I do. In short it is an [...]

Read the full article →

Basics of Headless Browser Testing with Zombie.js

October 22, 2012

Testing interaction with your site in an automated fashion can be hard, but fortunately there are tools out there to make it easier. I didn’t realize they existed until recently so I have been playing with them. One I am really liking is Zombie.js. To be honest I haven’t tested anything too difficult, but the [...]

Read the full article →