How to show the git branch in the terminal

Dany ParedesDany Paredes
1 min read

I'm a git Kraken lover but sometimes surf into my projects. I love to know in which branch I'm working.

Edit the .bash_profile and paste the following lines.

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "

Terminal with branch name

If you are using zsh

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
export PROMPT='%~ $(parse_git_branch) $ '

Have a nice day :)

1
Subscribe to my newsletter

Read articles from Dany Paredes directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dany Paredes
Dany Paredes

I'm passionate about front-end development, specializing in building UI libraries and working with technologies like Angular, NgRx, Accessibility, and micro-frontends. In my free time, I enjoy writing content for the Google Dev Library, This Is Angular Community, Kendo UI, and sharing insights here.