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.
First thing you need to make sure of is you have bash 4.0 or higher.
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.
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
Then you need to source the file by adding the following to your `~/.bashrc` or `~/.bash_profile`:
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.
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.
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.