Combine Git and Bash for better productivity

Posted by Harald Nezbeda on Wed 04 September 2019

I consider it good habit to use git via command line. A GUI is most likely to do things you don’t expect and make everything look like magic, and it is also counterproductive when your goal is to understand git.

But using the command line does not mean to it cannot be enhanced. After some twitter posts I learned that the ~/.bashrc can be extended to show the current branch in git. After a quick google search you will most likely end up with a code snippet like this:

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

Source: https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt

On a previous OS setup I also had a script that was marking the branch red for having changes, which was also useful. But of course there is more that can be done, so I taught what if is a package for this to avoid to try and error various snippets from the internet? And the good news is that it exists already and it is called Bash Git Promt

For Ubuntu the installation is straight forward by cloning the repository

git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt

Afterwards add it to the ~/.bashrc:

GIT_PROMPT_ONLY_IN_REPO=1
source ~/.bash-git-prompt/gitprompt.sh

Make sure to open a new terminal to load the new Bash configuration. This is what you get:

Git Bash Promt

The Bash will now display useful information such as the current branch name, changes in the workspace and much more.