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.

@heitoralthmann
Copy link

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

@joseluisq Thanks for this snippet. Worked!

@hamidiaf
Copy link

@hospino11 great!

@fugogugo
Copy link

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

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

Copy link

ghost commented Aug 8, 2022

Thanks @DhruboB

@Yurockkk
Copy link

https://gist.github.com/abdes-zakari/3eb232acd89ee697bf5d76d9862543d7

Thanks for sharing! this also worked for me.

@awani-mdsol
Copy link

+1

@IDK9911
Copy link

IDK9911 commented Jan 6, 2023

i wanna reference my https://gist.github.com/IDK9911/ec425bd83d3d2839c9884650a0dd1e8b
work as well.
If u wanna use an RPROMPT and uses zsh shell.
image

@ank-agar
Copy link

ank-agar commented Feb 9, 2023

Thank you!

@sanguinecromancer
Copy link

sanguinecromancer commented May 5, 2023

I experienced this one is the best:

export PS1='$(__git_ps1 " (%s)")'"$PS1"

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