Display git branch on your cli prompt

Gemini generated image w1n9pyw1n9pyw1n9

Want to display the current git branch on your cli prompt and go from ~/mywork$ to something like this ~/mywork [main]$ ?

Useful when working with multiple branches or projects. It makes it less confusing when switching between environments/tabs.

This is for bash and I haven’t tried on other cli. To include the current branch your in to your prompt :

Edit your .bashrc file (or equivalent config file for your cli). nano ~/.bashrc

Then add this at the end of your file

parse_git_branch() {
   git branch 2>/dev/null | grep '^*' | colrm 1 2
}
export PS1="\[\e[32m\]\w\[\e[91m\] [\$(parse_git_branch)]\[\e[00m\]\$ "

This includes color and formatting for the prompt and then resets the text color to the default.

Restart your terminal program or do a source ~/.bashrc to see the changes.

Enjoy!

Comments

Leave a Reply

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