Skip to content

Instantly share code, notes, and snippets.

@justintv
Created August 17, 2009 00:53
Star You must be signed in to star a gist
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$
@techy2493
Copy link

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

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

@8dcc
Copy link

8dcc 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

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

Good job! Thank you a lot!

@Abdel0104
Copy link

Thank you

@mxdpeep
Copy link

mxdpeep commented Oct 11, 2022 via email

@ariazarifian
Copy link

ariazarifian commented Oct 26, 2022

image

Working fine

Copy link

ghost commented Feb 12, 2023

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

he11 yeah! This color scheme is hands down the best on the internet. No contest. If this was mortal kombat this color code whispers "finish them"

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