Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Git Branch Name to Terminal Prompt (Linux/Mac)

Add Git Branch Name to Terminal Prompt (Linux/Mac)

image

Open ~/.bash_profile in your favorite editor and add the following content to the bottom.

# Git branch in prompt.

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

By MARTIN FITZPATRICK at http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/

Bonus for Fish shell users

If you are Fish shell user and you want speed up your Git workflow, try it out https://github.com/joseluisq/gitnow.

@DhruboB
Copy link

DhruboB commented Oct 25, 2020

This does not work anymore on Mac Catilina 10.15.6 :(

Update : Kudos to is0xjh25 ... it is now working in Mac catilina ..

add in your ~/.zshrc file

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

setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '

@is0xjh25
Copy link

is0xjh25 commented Oct 28, 2020

It does not work macOS Catilina 10.15.7. It shows up "\u@\h \W[\033[32m]$(parse_git_branch)[\033[00m] $" in terminal. Can anyone help? Thanks in advance!

@obymanyando
Copy link

obymanyando commented Oct 30, 2020

Thanks @DhruboB

@hospino11
Copy link

hospino11 commented Nov 1, 2020

This does not work anymore on Mac Catilina 10.15.6 :(

Update : Kudos to is0xjh25 ... it is now working in Mac catilina ..

add in your ~/.zshrc file

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

setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '

This worked for me! Catalina 10.15.7

Thanks @DhruboB

@ruiqiliu
Copy link

ruiqiliu commented Nov 16, 2020

@DhruboB. Great! It works on Mac Catilina 10.15.5

@rushitnshah
Copy link

rushitnshah commented Dec 16, 2020

Thanks

@skywhat
Copy link

skywhat commented Jan 4, 2021

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

Here is an example of what I came up with (zsh):

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

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Awesome. it's what I am looking for!

@meaghanturner
Copy link

meaghanturner commented Jan 22, 2021

thanks :D

@ltdev22
Copy link

ltdev22 commented Jan 25, 2021

COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'

How can I change these color values? is there any documentation listing all of the colors available and how to do it?

for catalina

@Chisomo-Chiweza
Copy link

Chisomo-Chiweza commented Jan 28, 2021

I am using Ubuntu 20.4 and since I couldn't find .bash_profile, edited .bashrc instead. worked.

@dlee0528
Copy link

dlee0528 commented Feb 10, 2021

Thank you !

@alpizano
Copy link

alpizano commented Feb 11, 2021

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
Here is an example of what I came up with (zsh):

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

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Awesome. it's what I am looking for!

Exactly what I needed, thank you man.

@alpizano
Copy link

alpizano commented Feb 16, 2021

when using this "function parse_git_branch()" script with zsh (Mac Catalina OS), should we be placing this script in our .zshenv or .zshrc or .zprofile?

@amirreza-yar
Copy link

amirreza-yar commented Mar 2, 2021

I change it :

parse_git_branch()
     git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1] /p'
}
export PS1="\[\e[93m\] 🠮\[\e[m\]\[\e[37m\] \W \[\e[m\]\[\e[1;31m\]\$(parse_git_branch)\e[m\]"

@priyesh-cloud1
Copy link

priyesh-cloud1 commented Apr 10, 2021

Awesome, thank you...

@johnbunnyan
Copy link

johnbunnyan commented Apr 22, 2021

This does not work anymore on Mac Catilina 10.15.6 :(
Update : Kudos to is0xjh25 ... it is now working in Mac catilina ..
add in your ~/.zshrc file

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

setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '

This worked for me! Catalina 10.15.7

Thanks @DhruboB

thanks a lot!! you saved me

@alisonwilbur
Copy link

alisonwilbur commented Apr 23, 2021

Thank you for sharing, and for everyone in the comments!

I had to add quotes around mine '$(parse_git_branch)' for it to work (zsh Catalina):

export PROMPT='%F{blue}[%D{%H:%M:%S}]%F{black}--%F{green}%.%F{black}--%F{cyan}'$(parse_git_branch)'%F{black}--%% '

@shahrokh-bahtooei
Copy link

shahrokh-bahtooei commented Apr 27, 2021

For a clean, modular approach, use git-prompt.sh:

  • It works perfectly on both zsh and bash.
  • It only shows branch when prompt is in a git repository.
  • It shows branch status via signs and colors.

https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

@kobataiwan
Copy link

kobataiwan commented Apr 30, 2021

thanks, awesome!

@Tekh-ops
Copy link

Tekh-ops commented May 10, 2021

Thanks

@sonyschan
Copy link

sonyschan commented Jul 13, 2021

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

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Awesome! Save my day!

@heitoralthmann
Copy link

heitoralthmann commented Aug 18, 2021

Thank you!

@Kalud-Z
Copy link

Kalud-Z commented Oct 17, 2021

thanks!!! +1

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
# git branch 2>/dev/null | /usr/bin/grep '^*' | colrm 1 2)"
}

PS1_date="\[\033[38;5;237m\]\d\[$(tput sgr0)\]\[\033[38;5;15m\]"
PS1_time="\[$(tput sgr0)\]\[\033[38;5;236m\]\t\[$(tput sgr0)\]\[\033[38;5;15m\]"
PS1_wdir="\[$(tput sgr0)\]\[\033[38;5;24m\]\W"
PS1_gitbranch="\e[38;5;204m\]\$(parse_git_branch)"
PS1_gt="\[$(tput bold)\]\[$(tput sgr0)\]\[\e[38;5;214m\]>"
PS1_other="\[$(tput sgr0)\]\[$(tput sgr0)\]\[\e[38;5;15m\]"

export PS1="${PS1_date} ${PS1_time} ${PS1_wdir}${PS1_gitbranch}${PS1_gt}${PS1_other} \[$(tput sgr0)\]"

image

@testradav thanks for the script. now it looks so much better !
I don't quite understand the code, Im wondering though, how do I change the color of PS1_gitbranch to yellow for example ?

@progzilla
Copy link

progzilla commented Nov 22, 2021

@joseluisq Thanks for this snippet. Worked!

@hamidiaf
Copy link

hamidiaf commented Apr 23, 2022

@hospino11 great!

@fugogugo
Copy link

fugogugo commented Jun 21, 2022

I already got working git branch in PROMPT before upgrading to Monterey , after upgrading it suddenly gone

apparently I only need to pick parse_git_branch method and replace the old PROMPT

thank you :)

@rburrowes
Copy link

rburrowes commented Jun 29, 2022

@NJRojas
Copy link

NJRojas commented Jul 26, 2022

This does not work anymore on Mac Catilina 10.15.6 :(
Update : Kudos to is0xjh25 ... it is now working in Mac catilina ..
add in your ~/.zshrc file

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

setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '

This worked for me! Catalina 10.15.7
Thanks @DhruboB

thanks a lot!! you saved me

Lovely, this was way easy to change colours :)

@awais-kaleem
Copy link

awais-kaleem commented Aug 8, 2022

Thanks @DhruboB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment