Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Display git branch in bash prompt
# If you work with git, you've probably had that nagging sensation of not knowing what branch you are on. Worry no longer!
export PS1="\\w:\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\$ "
# This will change your prompt to display not only your working directory but also your current git branch, if you have one. Pretty nifty!
# ~/code/web:beta_directory$ git checkout master
# Switched to branch "master"
# ~/code/web:master$ git checkout beta_directory
# Switched to branch "beta_directory"
# ~/code/web:beta_directory$
Copy link

cheesits456 commented Sep 11, 2021

I edited mine to show whether there's uncommitted changes on the current branch


# prompt

parse_git_bg() {
	[[ $(git status -s 2> /dev/null) ]] && echo -e "\e[43m" || echo -e "\e[42m"

parse_git_fg() {
	[[ $(git status -s 2> /dev/null) ]] && echo -e "\e[33m" || echo -e "\e[32m"

PS1="\n${FG_BLUE}╭─" # begin arrow to prompt
PS1+="${FG_MAGENTA}" # begin USERNAME container
PS1+="${BG_MAGENTA}${FG_CYAN}${FMT_BOLD}" # print OS icon
PS1+="${FG_WHITE}\u" # print username
PS1+="${FMT_UNBOLD} ${FG_MAGENTA}${BG_BLUE}" # end USERNAME container / begin DIRECTORY container
PS1+="${FG_GREY}\w " # print directory
PS1+="${FG_BLUE}${BG_CYAN}" # end DIRECTORY container / begin FILES container
PS1+="\$(find . -mindepth 1 -maxdepth 1 -type d | wc -l) " # print number of folders
PS1+="\$(find . -mindepth 1 -maxdepth 1 -type f | wc -l) " # print number of files
PS1+="\$(find . -mindepth 1 -maxdepth 1 -type l | wc -l) " # print number of symlinks
PS1+="\$(git branch 2> /dev/null | grep '^*' | colrm 1 2 | xargs -I BRANCH echo -n \"" # check if git branch exists
PS1+="\$(parse_git_bg) " # end FILES container / begin BRANCH container
PS1+="${FG_BLACK} BRANCH " # print current git branch
PS1+="${FMT_RESET}\$(parse_git_fg)\")\n" # end last container (either FILES or BRANCH)
PS1+="${FG_BLUE}╰ " # end arrow to prompt
PS1+="${FG_CYAN}\\$ " # print prompt
export PS1

Copy link

archenroot commented Sep 13, 2021

Probably the most rich solution I found so far:

Copy link

AndreasLuckert commented Sep 22, 2021

I would like to see my python virtual environment (venv) as well, as it is usually the case when using MINGW64-git-bash.

So previously, the venv "projectname-venv" was shown in parentheses on the left, but after applying some fancy PS1-command to change the style, coloring and make the current git-branch appear on the right, the venv is missing afterwards.

(projectname-venv) andylu@hd1pcms0347:/mnt/c/Users/username/Projects/projectname$
PS1='\[\033[0;32m\]\u\[\033[0;36m\] @ \[\033[0;36m\]\h \w\[\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\[\033[0;32m\] \$\[\033[0;32m\] ▶\[\033[0m\] '

andylu @ hd1pcms0347 /mnt/c/Users/username/Projects/projectname (feature/story-43052-dev)
└─ $ ▶ ls

I'd like to have a PS1-command including the venv.

Copy link

Lulalaby commented Oct 2, 2021


How do I let it display correctly?

Windows Terminal - SSH

Copy link

cheesits456 commented Oct 5, 2021

@Lulalaby You need to set the terminal font to a nerd font (patched fonts that replace unused characters with different icons). You can get nerd fonts from The one I'm using is FantasqueSansMono

Copy link

Lulalaby commented Oct 5, 2021


Copy link

fbakhda commented Oct 7, 2021

I just recently started using this and works perfectly.

git_branch() {

if [ git branch 2>/dev/null | grep '^*' | colrm 1 2 | wc -l -eq 1 ];
echo -n "("
echo -n git branch 2>/dev/null | grep '^*' | colrm 1 2
echo -n ")"


PS1='[\033[0;32m][\033[0m\033[0;32m]\u[\033[0;36m] @ [\033[0;36m]\h \w[\033[0;32m] $(git_branch)\n[\033[0;32m]└─[\033[0m\033[0;32m] $[\033[0m\033[0;32m] ▶[\033[0m] '

This will not have () if there is no branch found.
And it will write with a () if there is a branch.

Copy link

er314 commented Jan 3, 2022

Optimization wrt slow prompt rendering :
How to make the prompt display INSTANTLY, whatever the complexity of the git state computation ?
-> by NOT performing any git state computation, UNLESS the working directory has changed, OR git command was run.

PROMPT_COMMAND='history1=`history 1`;
if [[ $PREVIOUS_PWD != $PWD || ( $history1 == *" git "* && $history1 != $PREVIOUS_CMD ) ]]; 
then GITBRANCH=`__git_ps1`;  # or whatever git state computation commands

PS1='\[\033[32m\]\u@\h \[\033[33m\]\w\[\033[36m\]$GITBRANCH\[\033[0m\]\n$ '
# or whatever prompt rendering, as long as it embeds $GITBRANCH which has been conditionally computed above

Copy link

techy2493 commented Feb 20, 2022

Could any of you share the fonts you are using in your terminals? Especially the ones with the file and folder icons?

Copy link

holmesrichards commented Mar 8, 2022

-> by NOT performing any git state computation, when the working directory has NOT changed.

But then the prompt doesn't change when you switch branches.

Copy link

er314 commented Mar 9, 2022

-> by NOT performing any git state computation, when the working directory has NOT changed.

But then the prompt doesn't change when you switch branches.

You're correct.
So let's improve & rephrase the optimization :
"-> by NOT performing any git state computation, UNLESS the working directory has changed, OR git command was run."

I updated my original proposed solution accordingly.

Copy link

r4v10l1 commented Apr 7, 2022

I created my own version for displaying the git branch in prompt starting from @vankasteelj reply (wrote on Mar 7, 2016).


It is a simple solution that includes branch status color. Take a look:

parse_git_bg() {
  if [[ $(git status -s 2> /dev/null) ]]; then
    echo -e "\033[0;31m"
    echo -e "\033[0;32m"

PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;34m\]@\[\033[0;34m\]\h \w\[$(parse_git_bg)\]$(__git_ps1)\n\[\033[0;32m\]\$\[\033[0m\] 

Based. Thanks.

Copy link

iaacornus commented Apr 11, 2022

Screenshot from 2022-04-12 00-16-23

1 as True or correct command, and if it is 0, then it is false or wrong.

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

export PS1="╭─╼[\[\e[1;36m\]\w\[\e[0m\]]-(\`if [ \$? = 0 ]; then echo \[\e[32m\]1\[\e[0m\]; else echo \[\e[31m\]0\[\e[0m\]; fi\`)-[\[\e[1;32m\]\h\[\e[0m\]]\n╰─ \u\$(if git rev-parse --git-dir > /dev/null 2>&1; then echo '@git:('; fi)\[\e[1;34m\]\$(parse_git_branch)\[\e[0m\]\$(if git rev-parse --git-dir > /dev/null 2>&1; then echo ')'; fi) >> "


Copy link

RedDofamine commented Apr 11, 2022

Good job! Thank you a lot!

Copy link

Abdel0104 commented Apr 14, 2022

Thank you

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