Monday, December 23, 2013

Current Git branch name in Terminal

To know which branch is active now, we need to check git status. But Here i describe a technique that enable your terminal to show current branch.

If you use linux for OS then just add the following code to .bashrc file in your home directory:

function fetch_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
 
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"
 
PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(fetch_git_branch)$NO_COLOR\$ "
 
Once you put this code on the specified location, then you need to reload your .bashrc or re-enter your terminal window by closing previous window.

To reload .bashrc, follow this command:

source ~/.bashrc
  
Also, If your terminal is not default colour activated  when you open terminal window, then just add following line to the .bashprofile in your home directory:


[[ -s ~/.bashrc ]] && source ~/.bashrc
 
If you are on mac, you should put this code to .bash_profile in your home directory. For windows, it is set by default. No need to add extra care!