Skip to content

Instantly share code, notes, and snippets.

@eliotsykes
Last active April 28, 2023 12:18
Show Gist options
  • Star 64 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save eliotsykes/47516b877f5a4f7cd52f to your computer and use it in GitHub Desktop.
Save eliotsykes/47516b877f5a4f7cd52f to your computer and use it in GitHub Desktop.
Git Aware Bash Prompt

In Terminal

mkdir ~/.bash

Copy the raw git-prompt.sh file from git contrib in to the ~/.bash directory: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

Inside ~/.bashrc or ~/.bash_profile (choose the file where you normally put any bash customizations/setup), add the lines:

source ~/.bash/git-prompt.sh # Show git branch name at command prompt
export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

# Terminal Prompt:
# Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
export PROMPT_COMMAND='__git_ps1 "\w" "\n\\\$ "' # Git branch (relies on git-prompt.sh)

As long as you're inside a git repo, your Bash prompt should now show the current git branch in color signifying if its got uncommitted changes.

@fragmuffin
Copy link

I've been having fun with this the past few days commute to work...

export GIT_PS1_SHOWDIRTYSTATE=true      # staged '+', unstaged '*'
export GIT_PS1_SHOWUNTRACKEDFILES=true  # '%' untracked files
export GIT_PS1_SHOWUPSTREAM="auto"      # '<' behind, '>' ahead, '<>' diverged, '=' no difference
export GIT_PS1_SHOWSTASHSTATE=true      # '$' something is stashed

function __prompt_command() {
    local ERRORCODE="$?"
    PS1="${debian_chroot:+($debian_chroot)}"

    # Errorcode (conditional)
    if [ ${ERRORCODE} != 0 ]; then
        PS1+="\e[90m    $(echo -e '\u2570\u2500\u2770')\e[1;31m$ERRORCODE\e[90m$(echo -e '\u2771')\e[0m\n"
    fi

    # Main line
    local c="$(echo -e '\u256d\u2500')"
    if [[ "$(dirs -p | wc -l)" != "1" ]] ; then
        local c="$(echo -e '\u2934') "
    fi
    PS1+="\e[90m$c\e[0m"
    if [[ ! -z "${VIRTUAL_ENV}" ]]; then
        PS1+="\e[90m$(echo -e '\u2770')\e[32m$(basename $VIRTUAL_ENV)\e[90m$(echo -e '\u2771')\e[0m"
    fi
    PS1+=" \e[33;1m\w\e[m"
    PS1+="\$(__git_ps1)"

    # Command Line
    PS1+="\n\e[90m$(echo -e '\u2570\u2500\u2bc8') \e[0m"
}

export PROMPT_COMMAND=__prompt_command

results in:
image

@hky404
Copy link

hky404 commented Jun 26, 2019

why does this remove my actual directory path along with my virtualenv name, now I am only left with $ in my prompt. any way to fix it?

@AlexRuizE
Copy link

works great!

@alfredoh1234
Copy link

Very happy with it!
Thanks!

@wangyu-okta
Copy link

This has worked well for a while, until recently when it significantly slows down everything - eg, when I "more PATH/TO/any-file", then "q" to quit - the quitting takes 5 seconds!!! Everything starts to take long.
It might not be the issue with this tool itself - but I do not have to have my prompt show me the GIT info. Hence I just disabled it :(

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