bash git prompt

Aug 9, 2014   #bash  #git 

In a team environment, it’s common to use GIT branches. However this means you need to pay more attention to what branch you’re in before doing commits and pushes. You can of course use the git branch command.

However you might skip that step and accidentally plonk your new untested code directly into master or similar. Much better then to improve visibility by adding the current branch to your bash prompt.

You’ll need bash_completion and git installed.

Edit your bashrc

vim ~/.bashrc

Add a section to import the relevant git bash_completion file

# linux if [ -f /etc/bash_completion.d/git ]; then . /etc/bash_completion.d/git fi # mac #if [ -f /usr/local/git/contrib/completion/git-prompt.sh ]; then # . /usr/local/git/contrib/completion/git-prompt.sh #fi

Update your git prompt to something like the following

# linux # show pending (+ = new files, * = changed files) state export GIT_PS1_SHOWDIRTYSTATE=1 export PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[0;34m\]\w\[\e[m\]\[\e[0;31m\]$(__git_ps1)\[\e[m\] $ '

Source the file

# linux source ~/.bashrc

And browse to a path with a git repo