Show the current git branch in linux terminal

Branches are a speciality of GIT where one can make changes without disturbing the full or the master repo. However, it is difficult to remember which branch we are present in through linux terminal. With the following code, we can easily get to know the branch in linux terminal.

Firstly, open file .bashrc present in your home folder

$ vim ~/.bashrc as superuser

Press I and add the following code anywhere in bashrc file

alias ls='ls --color=auto'

PS1="\[\e[1;32m\][\u@\h \W]\$(parse_git_branch)\[\e[0m\] "

 

function parse_git_branch () {

       git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

}

Once done press the ‘esc’ key and save and exit the document. (by typing :wq)

After the file is saved enter the following code to activate it

$ source ~/.bashrc

Once the code is run, we get the directory as below

[username@host reponame] (branch_name)

Add a Comment

Your email address will not be published. Required fields are marked *