Skip to content

Instantly share code, notes, and snippets.

Embed
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$
@cheesits456
Copy link

cheesits456 commented Dec 8, 2020

its because im using nerdfont, it replaces those characters with icon glyphs

@mrcat323
Copy link

mrcat323 commented Dec 9, 2020

I edited a bit @vankasteelj's stuff and made something like zsh, to react to git's changes
image
If there are some changes then the color changes to orange or whatever, like in zsh (or fish also, I don't know)
so, to do that, you need to create a function

function changes_in_branch() { 
    if [ -d .git ]; then
	if expr length + "$(git status -s)" 2>&1 > /dev/null; then     
	    echo -ne "\033[0;33m$(__git_ps1)\033[0m"; 
	else
	    echo -ne "\033[0;32m$(__git_ps1)\033[0m"; fi; 
    fi
}

and then simply just add that function to the PS1 stuff.
Just like this:

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

@Gravitcapa-py
Copy link

Gravitcapa-py commented Dec 19, 2020

Works! Thank you! Ubuntu 20.4

@cheesits456
Copy link

cheesits456 commented Dec 19, 2020

@cheesits456 I really like yours, although it is a bit off in directories that aren't git, shows a lot of Chinese like markings, (lool and some numbers too, can I know what they stand for ? thanks ).
I think the first number is for folders, the next is for text files .....
GIT

@stealthman22 I just realized I never actually gave a proper answer to your question, sorry :P Nerd Font is a project that makes patched versions of popular fonts, replacing a ton of unused characters with icons - you can download one from https://www.nerdfonts.com/font-downloads, then put the ttf file in ~/.fonts, then set it as your terminal's font - once that's done, the icons will show up properly.

As for what they represent, I'll go left to right. The one that looks like a purple E becomes a filled left angle bracket once you apply a nerd font. Next to that is my OS icon - if you run something other than Arch, you can search your OS at https://www.nerdfonts.com/cheat-sheet and copy/paste the character into your .bashrc file in place of the Arch icon. After the username there's a filled right angle bracket, then your current directory in plaintext, then another filled right angle bracket. After that is a folder icon, then a number equal to the number of folders in your current directory. Then there's a file icon and a link icon, both with numbers beside them, which represent the number of files and symbolic links in your current directory, respectively. If you're in a git directory it'll show a git branch icon and print the name of your current branch. The rest of the icons from the nerd font are just more of the shaped icons for styling.

Here's a screenshot that shows the relation between the numbers and the types of files in the current directory:

image

Hope this helps <3

@b1ackviking
Copy link

b1ackviking commented Feb 24, 2021

Here is mine PS1:

function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working tree clean" ]] && echo "*"
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/(\1$(parse_git_dirty))/"
}
export PS1='\[\e[\033[01;34m\]\u@\h \[\e[38;5;211m\]\W\[\e[\033[38;5;48m\] $(parse_git_branch)\[\e[\033[00m\]\$ '

image

@alecszaharia
Copy link

alecszaharia commented Mar 17, 2021

My two cents..

# prompt
FMT_BOLD="\e[1m"
FMT_RESET="\e[0m"
FMT_UNBOLD="\e[21m"
FG_BLACK="\e[30m"
FG_BLUE="\e[34m"
FG_CYAN="\e[36m"
FG_GREEN="\e[32m"
FG_MAGENTA="\e[35m"
FG_RED="\e[31m"
FG_WHITE="\e[97m"
BG_BLUE="\e[44m"
BG_GREEN="\e[42m"
BG_MAGENTA="\e[45m"

export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWCOLORHINTS=1

export PS1=\
"\n\[${BG_GREEN}\] \[${FG_RED}\] \[${FG_BLACK}\]\u \[${FG_GREEN}${BG_BLUE}\] "\
"\[${FG_BLACK}\]\w \[${FMT_RESET}${FG_BLUE}\]"\
'$(__git_ps1 "\[${BG_MAGENTA}\] \[${FG_WHITE}\] %s \[${FMT_RESET}${FG_MAGENTA}\]")'\
"\n \[${FG_GREEN}\]╰ \[${FG_CYAN}\]\$ \[${FMT_RESET}\]"

image

@javahaxxor
Copy link

javahaxxor commented Apr 23, 2021

image
Notice the squares at the start of the lines, why am I getting those ?

I love it otherwise

@nononsensetekkie
Copy link

nononsensetekkie commented Apr 23, 2021

FMT_BOLD="\e[1m"
FMT_RESET="\e[0m"
FMT_UNBOLD="\e[21m"
FG_BLACK="\e[36m"
FG_BLUE="\e[34m"
FG_CYAN="\e[36m"
FG_GREEN="\e[32m"
FG_MAGENTA="\e[35m"
FG_RED="\e[31m"
FG_WHITE="\e[97m"
BG_BLUE="\e[44m"
BG_GREEN="\e[42m"
BG_MAGENTA="\e[45m"

@vuon9, I really liked your functions to shorten the directory path in the prompt.
However, since you defined the colors without the closing brackets, I found that whenever I brought back the previous command and tried to edit the command, I could not go back to the beginning of the line. There were a few dangling characters padding the left side of the command.

To fix that issue, I combined the prompt at https://gist.github.com/justintv/168835#gistcomment-3554316 from @cheesits456, replacing Nerd font characters with available emoji icons, to arrive at my prompt as below.

screenshot 1
screenshot 2

# Adaptation of
# - functions from https://dev.to/vuong/let-s-add-cygwin-into-windows-terminal-and-customize-it-for-development-looks-1hp8
# - prompt arrangement from https://gist.github.com/justintv/168835#gistcomment-3554316

# Just shorten the cygwin path
function __short_wd_cygwin() 
{
    num_dirs=3
    newPWD="${PWD/#$HOME/~}"
    if [ $(echo -n $newPWD | awk -F '/' '{print NF}') -gt $num_dirs ]; then
        newPWD=$(echo -n $newPWD | awk -F '/' '{print $1 "/.../" $(NF-1) "/" $(NF)}')
    fi

    echo -n $newPWD
}

# Convert shorten path and shorten the Windows path
function __short_wd_cygpath() 
{
    num_dirs=3
    newPWD=$(cygpath -C ANSI -w ${PWD/#$HOME/~})
    if [ $(echo -n $newPWD | awk -F '\\' '{print NF}') -gt $num_dirs ]; then
        newPWD=$(echo -n $newPWD | awk -F '\\' '{print $1 "\\...\\" $(NF-1) "\\" $(NF)}')
    fi

    echo -n $newPWD
}

FFMT_BOLD="\[\e[1m\]"
FMT_DIM="\[\e[2m\]"
FMT_RESET="\[\e[0m\]"
FMT_UNBOLD="\[\e[22m\]"
FMT_UNDIM="\[\e[22m\]"
FG_BLACK="\[\e[30m\]"
FG_BLUE="\[\e[34m\]"
FG_CYAN="\[\e[36m\]"
FG_GREEN="\[\e[32m\]"
FG_GREY="\[\e[37m\]"
FG_MAGENTA="\[\e[35m\]"
FG_RED="\[\e[31m\]"
FG_WHITE="\[\e[97m\]"
BG_BLACK="\[\e[40m\]"
BG_BLUE="\[\e[44m\]"
BG_CYAN="\[\e[46m\]"
BG_GREEN="\[\e[42m\]"
BG_MAGENTA="\[\e[45m\]"

export PS1=\
"\n${FG_BLUE}╭─${FG_MAGENTA}${BG_MAGENTA}${FG_CYAN}${FMT_BOLD}\d ${FG_WHITE}\t${FMT_UNBOLD} ${FG_MAGENTA}${BG_BLUE}"\
"${FG_GREY}\$(__short_wd_cygwin) ${FG_BLUE}${BG_CYAN}"\
"${FG_BLACK}📂 \$(find . -mindepth 1 -maxdepth 1 -type d | wc -l) "\
"📄 \$(find . -mindepth 1 -maxdepth 1 -type f | wc -l) "\
"🔗 \$(find . -mindepth 1 -maxdepth 1 -type l | wc -l) "\
"${FMT_RESET}${FG_CYAN}"\
"\$(git branch 2> /dev/null | grep '^*' | colrm 1 2 | xargs -I BRANCH echo -n \"${BG_GREEN}${FG_BLACK}🔀 BRANCH ${FMT_RESET}${FG_GREEN}\")"\
"\n${FG_BLUE}╰▶${FG_CYAN}🍄 ${FMT_RESET}"

@vuon9
Copy link

vuon9 commented Apr 24, 2021

@nononsensetekkie oh wow, I got your point. I love the improvement too!!!

@joejoseph00
Copy link

joejoseph00 commented Apr 24, 2021

This is for Ubuntu but probably will work for other distros that use the DejaVuMono.ttf font as the default.
This is for those that don't want to fiddle with changing fonts, suggest this:

#!/bin/bash
cwd=`pwd`
current_username=`whoami`
export $current_username
export HISTSIZE=99999
export HISTFILESIZE=99999
# prompt
FMT_BOLD="\[\e[1m\]"
FMT_DIM="\[\e[2m\]"
FMT_RESET="\[\e[0m\]"
FMT_UNBOLD="\[\e[22m\]"
FMT_UNDIM="\[\e[22m\]"
FG_BLACK="\[\e[30m\]"
FG_BLUE="\[\e[34m\]"
FG_CYAN="\[\e[36m\]"
FG_GREEN="\[\e[32m\]"
FG_YELLOW="\[\e[33m\]"
FG_GREY="\[\e[37m\]"
FG_MAGENTA="\[\e[35m\]"
FG_RED="\[\e[31m\]"
FG_WHITE="\[\e[97m\]"
BG_BLACK="\[\e[40m\]"
BG_BLUE="\[\e[44m\]"
BG_CYAN="\[\e[46m\]"
BG_GREEN="\[\e[42m\]"
BG_YELLOW="\[\e[43m\]"
BG_MAGENTA="\[\e[45m\]"

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}${FMT_BOLD} ${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+="${FG_BLACK}"
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+="${FMT_RESET}${FG_CYAN}"
PS1+="\$(git branch 2> /dev/null | grep '^*' | colrm 1 2 | xargs -I BRANCH echo -n \"" # check if git branch exists
PS1+="${BG_YELLOW}▶ " # end FILES container / begin BRANCH container
PS1+="${FG_WHITE}🔀${FMT_BOLD} BRANCH " # print current git branch
PS1+="${FMT_RESET}${FG_YELLOW}\")▶\n " # end last container (either FILES or BRANCH)
PS1+="${FG_BLUE}╰❯ " # end arrow to prompt
PS1+="${FG_CYAN}\\$ " # print prompt
PS1+="${FMT_RESET}"
export PS1

function lscolor() {
while IFS=$'\t' read -r -d '' perms name
do
    echo "${perms:0:1}$(tput bold; tput setaf 1)${perms:1:3}$(tput setaf 4)${perms:4:3}$(tput setaf 2)${perms:7:3}$(tput sgr0) ${name}"
done < <(find . -maxdepth 1 -printf "%M\t%f\0")
}
alias ll="lscolor"

screenshot:

image

@TheCoderJT
Copy link

TheCoderJT commented Apr 25, 2021

How does one make the terminal look like this?

@joejoseph00
Copy link

joejoseph00 commented Apr 25, 2021

How does one make the terminal look like this?

Copy the code that I pasted into /home/your_user_name/.bash_profile

Then restart the terminal

@indrajeetgour
Copy link

indrajeetgour commented Jun 2, 2021

how do I can use this icon in my PS1, which is coming with master from the mentioned screenshot, feels like not able to decode my OS(having windows 10)

https://user-images.githubusercontent.com/35486894/101484925-387de700-390f-11eb-8462-b562accf7ff1.png

@juansedo
Copy link

juansedo commented Jul 30, 2021

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

image

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"
  else
    echo -e "\033[0;32m"
  fi
}

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\] 

@itachi-19
Copy link

itachi-19 commented Sep 11, 2021

I'll leave this here:

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

capture_du_2016-03-07_19-13-07

WOOOW..Thanks a lot for this. :D

@cheesits456
Copy link

cheesits456 commented Sep 11, 2021

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

image

# prompt
FMT_BOLD="\[\e[1m\]"
FMT_DIM="\[\e[2m\]"
FMT_RESET="\[\e[0m\]"
FMT_UNBOLD="\[\e[22m\]"
FMT_UNDIM="\[\e[22m\]"
FG_BLACK="\[\e[30m\]"
FG_BLUE="\[\e[34m\]"
FG_CYAN="\[\e[36m\]"
FG_GREEN="\[\e[32m\]"
FG_GREY="\[\e[37m\]"
FG_MAGENTA="\[\e[35m\]"
FG_RED="\[\e[31m\]"
FG_WHITE="\[\e[97m\]"
BG_BLACK="\[\e[40m\]"
BG_BLUE="\[\e[44m\]"
BG_CYAN="\[\e[46m\]"
BG_GREEN="\[\e[42m\]"
BG_MAGENTA="\[\e[45m\]"
BG_RED="\[\e[41m\]"

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+="${FG_BLACK}"
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+="${FMT_RESET}${FG_CYAN}"
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
PS1+="${FMT_RESET}"
export PS1

@archenroot
Copy link

archenroot commented Sep 13, 2021

Probably the most rich solution I found so far: https://github.com/diogocavilha/fancy-git

@AndreasLuckert
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.

@Lulalaby
Copy link

Lulalaby commented Oct 2, 2021

@cheesits456

How do I let it display correctly?
image

Windows Terminal - SSH

@cheesits456
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 https://www.nerdfonts.com/font-downloads. The one I'm using is FantasqueSansMono

@Lulalaby
Copy link

Lulalaby commented Oct 5, 2021

thanks!

@fbakhda
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 ];
then
echo -n "("
echo -n git branch 2>/dev/null | grep '^*' | colrm 1 2
echo -n ")"
fi

}

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.

@er314
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
fi; 
PREVIOUS_PWD=$PWD; PREVIOUS_CMD=$history1;'

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

@techy2493
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?

@holmesrichards
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.

@er314
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.

@r4v10l1
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).

image

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"
  else
    echo -e "\033[0;32m"
  fi
}

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.

@iaacornus
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) >> "


PROMPT_DIRTRIM=2

@RedDofamine
Copy link

RedDofamine commented Apr 11, 2022

Good job! Thank you a lot!

@Abdel0104
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