A neat UNIX trick for your bash profile

Hi folks! I have been spending a lot of time with KStars lately. I will write a detailed account on the work done till now, but here’s something I found interesting. This, I think is a handy ‘precautionary’ trick that every newbie should implement to avoid pushing to the wrong git repo/branch.

Here’s what you do. Open up the konsole and type cd ~ (This should take you to your home directory. Now what we need to do is add one line to your .bashrc file.

$nano .bashrc

Opens up your .bashrc file in the nano editor (you could choose vim, or emacs too).

Add this line export PS1=’\W$(__git_ps1 “(%s)”)> to the part of the ‘if block’ that pertains to bash completion. In my case this is how my .bashrc looks.

  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
        export PS1=’\W$(__git_ps1 “(%s)”)> ‘
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion

What this does is that it changes the text on your konsole. Whenever you enter a git repository, the text on your console reads the repo name with the git branch you are on currently (hence the %s to the __git_ps1 variable). This is how my kstars repository now looks.

~> cd Projects/kstars/
kstars(gsoc2015-constellationart)> git branch
* gsoc2015-constellationart
kstars(gsoc2015-constellationart)> git checkout master
Switched to branch ‘master’
Your branch is up-to-date with ‘origin/master’.

Now you can always know what branch you are on, without typing git branch. Pretty neat! 😎