In this post I demonstrate how to show the status and branch of the current git repository in the bash prompt. This can be a time-saver for anyone who switches branches constantly.
Table of Contents
First follow the set up instructions to change the bash prompt. In
the example below I have added a new file
bashprompt.sh to the repository.
This new file is untracked, so the prompt will be red. Next I add the new file
staged for committing, leading to a yellow prompt. Finally, I commit the new
file. At this point, all files are tracked and committed, so the prompt is
To change the bash prompt we need to define a function which grabs the branch and status of the git repository of the current directory. You can download the following script from one of my github repos which defines the necessary function.
Next source the script to change the bash prompt.
If you want to change the prompt, edit the variable defintion for PS1. For example to include the hostname and username in the prompt: