How to display bash prompt with current git branch

How to display bash prompt with current git branch

Here I am using bash terminal. We need .bashrc file which will be mostly present in your home directory, .bashrc will have details about the customization, preferences of your terminal, .bashrc file called as bash profile of your terminal

If you are looking for simple bash prompt in your terminal with pwd and current branch, please follow these steps

You can use any text editor to open Vim, Nano etc.

Check for the line PS1, which is responsible for bash prompt.

You can comment or remove the line and replace it with below code. Updating PS1 with below code only Prompt Current Working Directory in Blue, Current Branch in Yellow and text will be white with bold

PS1="\[\e[0;1;34m\]\W \[\e[0;1;33m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\[\e[0;7;0;37m\]$ "

If not you can use export command, it will added at end of the .bashrc file

export PS1="\[\e[0;1;34m\]\W \[\e[0;1;33m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\[\e[0;7;0;37m\]$ "

For check use cat command

cat ~/.bashrc

Using above PS1 in Home directory - just ~ in prompt - no nonsense

With main branch in a folder

After checkout to another branch change in the branch name

If your looking for more customization of your bash terminal, you can check this gist https://gist.github.com/justintv/168835

Hope this article helps you

Thanks for scrolling through :)