Skip to content

Instantly share code, notes, and snippets.

@justintv
Created August 17, 2009 00:53
Show Gist options
  • Save justintv/168835 to your computer and use it in GitHub Desktop.
Save justintv/168835 to your computer and use it in GitHub Desktop.
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$
@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"

@Jsscrdng
Copy link

Jsscrdng commented Aug 4, 2023

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

Works perfect in a Laptop with WSL and Ubuntu. I recently moved to a Macbook with M1, tried to use the same logic unsucessfully. After some tweaks I edited the changes_in_branch() function to make it work in my Mac. This is the result:

function changes_in_branch() {
    if [ -d .git ]; then
        ChangesStr="$(git status -s)"
        if [[ -n $ChangesStr ]]; then
            echo -ne "\033[0;33m$(__git_ps1)\033[0m";
        else
            echo -ne "\033[0;32m$(__git_ps1)\033[0m"; fi;
    fi
}

I had also to install bash-git-prompt in order to make work __git_ps1

@abnerrizzi
Copy link

do I do it like above ? Dont mind the emoji, I know its representing the user's machine name.

It shows only the repository name, (not including its path if it were to go into a subfolder), git:(current branch), and some x. Tried to search online but could not find any.

So in short (user)@(repo name only)git:(current branch)(space)x

Sorry I forgot I am using zsh.

have you tried zsh+omz ?
https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

@parthsawant2001
Copy link

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

I love it otherwise

Heyy @javahaxxor
when i 'ls' my files and folders appear vertically. How do i make it look horizontally like you have in the above image??

@ContbustableLemon
Copy link

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

I love it otherwise

What is this for?

@harshakp06
Copy link

PS1="\[\e[0;1;34m\]\W \[\e[0;1;33m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\[\e[0;7;0;37m\]$ "

Try this, for minimal text on terminal, with present working directory in blue and branch in yellow color

@hansfn
Copy link

hansfn commented Mar 25, 2024

[...] git branch 2>/dev/null | grep '^*' | colrm 1 2 [...]

Or with a newer Git:

git branch --show-current 2>/dev/null

It's not shorter, but easier to understand ;-)

@harshakp06
Copy link

git branch --show-current 2>/dev/null

Is not working with newer git

@hansfn
Copy link

hansfn commented Mar 26, 2024

This has been supported since at least Git 2.28.0, but let us not spend any more time on this. Your solution works for all versions :-)

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