Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active November 25, 2024 08:03
Show Gist options
  • Save joseluisq/1e96c54fa4e1e5647940 to your computer and use it in GitHub Desktop.
Save joseluisq/1e96c54fa4e1e5647940 to your computer and use it in GitHub Desktop.
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.

@unknowntocka
Copy link

thanks

@kaziislam
Copy link

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} $ '

Thanks... this worked for me on my mac for both iTerm and Terminal 👍👍

@him11
Copy link

him11 commented Aug 14, 2023

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

This worked for me too. I'm using macOS 13.2.1. Thank you. 😃

@adiletmaks
Copy link

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

This worked for me too. I'm using macOS 13.2.1. Thank you. 😃

thanks!

@pchokhaw
Copy link

pchokhaw commented Mar 8, 2024

I get an error : Badly Placed 's() while using this parse_git_function () in my Konsole terminal , can anyone please help me fix it ?

@jcmloiacono
Copy link

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 is right for me on MACOs 14.5

@astes-consulting
Copy link

Nice. Excellent !

@PlotArmor-Dev
Copy link

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

Hey man i am a complete beginner, i noticed my machines name disappeared, is there a way to bring it back? and i want to use hex code for the colors how do i do that? i tried replacing the color names but it didnt work.

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