Dev Life
·1 min read·↗

How to show the git branch in the terminal

Main cover illustration for article: How to show the git branch in the terminal
Summarize with AI:

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
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 :)

Part of the Dev Life Series

Reflections on career, soft skills, productivity, and the human side of programming.

View Entire Series

Related Articles

Share this article

If you found this guide helpful, consider sharing it with your team or fellow developers.


Real Software. Real Lessons.

I share the lessons I learned the hard way, so you can either avoid them or be ready when they happen.

User avatar
User avatar
User avatar
User avatar
+13K

Join 13,800+ developers and readers.

No spam ever. Unsubscribe at any time.