My scrapbook about almost anything I stumble upon in my tech world. If you find anything useful don't forget to give thumbs-up :)

Breaking

Sunday, February 9, 2020

How to add Git branch in your linux terminal prompt (PS1)

It would be great if we can see the git branch name on terminal whenever working with git, it will save us little time to play around some other git command rather than  git branch command.

We can set up our terminal shell to show the git branch as well by modifying the variable PS1. Your user home directory have a hidden file name .profile (dot profile) which have this variable, you need to tweak it little bit to fit in our ask.

To get which shell you are using, execute below command -
echo $SHELL

If you are using Korn Shell (ksh):

# Setting up PS1 value
export HOST=`hostname -a`
export GIT_BRANCH="$(print "\033[35m (\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)) \033[00m")"
export PS1="\${USER}@\${HOST}:\${PWD}${GIT_BRANCH}\$ "


If you are using Bash Shell (bash):

# Setting up PS1 value
parse_git_branch() {git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'}
export PS1="[\u@\h \[\e[32m\]\W \[\e[91m\]\$(parse_git_branch)\[\e[00m\]]$ "


Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Telegram Group

No comments:

Post a Comment

Disclaimer

The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.