Friday, May 11, 2018

Adding the branch name of the Current project in Command Prompt

Yeah, adding the branch name in the command prompt might be an easy task in third-party Command Editors, but not so straightforward in default editors.

So, I did some research and found the snippet that helps us to place branch name, in the prompt.

Here are the steps:

  1. Open .bash_profile of the user
    • vi ~/.bash_profile
  2. Add the following code in the file

    1
    2
    3
    4
    5
    6
    # Git branch in prompt.
    parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    
    export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
    
  3. Save the file and exit the command prompt.
  4. Restart the command prompt and you will be having the branch.
If everything goes well you will get the output something like this:


Thanks 
and 
Happy Coding :)