Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

@syscools

This comment has been minimized.

Copy link

syscools commented Mar 7, 2017

works so far... Thanks.!

@jbrizbee

This comment has been minimized.

Copy link

jbrizbee commented Apr 7, 2017

Thanks! Working great for me

@karlpokus

This comment has been minimized.

Copy link

karlpokus commented Aug 11, 2017

Thanks for sharing! These are pretty handy too

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWUPSTREAM="auto"
@shairyar

This comment has been minimized.

Copy link

shairyar commented Sep 11, 2017

Thanks, works great.

@polaroidkidd

This comment has been minimized.

Copy link

polaroidkidd commented Dec 1, 2017

Hi

Thanks! Works great so far. I've only one question.
When I use the provided git-prompt.sh my input in bash is on the next line as such
/mnt/c
$

I've tried amending the git-prompt.sh but it still keeps happening. Do you happen to have an idea why that could be?

EDIT:

Found it! Had to remove the ´\n´ in export PROMPT_COMMAND='__git_ps1 "\w" "\n\\\$ "'

@rijans

This comment has been minimized.

Copy link

rijans commented Jan 29, 2018

Thanks! Working

@midlan

This comment has been minimized.

Copy link

midlan commented Feb 13, 2019

I use this:

# GIT bash integration
if [[ -e /usr/lib/git-core/git-sh-prompt ]]; then

        source /usr/lib/git-core/git-sh-prompt

        export GIT_PS1_SHOWCOLORHINTS=true
        export GIT_PS1_SHOWDIRTYSTATE=true
        export GIT_PS1_SHOWUNTRACKEDFILES=true
        export GIT_PS1_SHOWUPSTREAM="auto"
        # PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'

        # use existing PS1 settings
        PROMPT_COMMAND=$(sed -r 's|^(.+)(\\\$\s*)$|__git_ps1 "\1" "\2"|' <<< $PS1)

fi
@kfcobrien

This comment has been minimized.

Copy link

kfcobrien commented Apr 7, 2019

Here is a simple clean version that I use: link

@fragmuffin

This comment has been minimized.

Copy link

fragmuffin commented May 23, 2019

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

AlexRuizE commented Jan 14, 2020

works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.