DEV Community

Vlastimil Pospichal
Vlastimil Pospichal

Posted on

Git Prompt in Bash

All in one picture

Screenshot of use

Source code

# ~/.bashrc

__git_status() {
    STATUS=$(git status 2>/dev/null |
    awk '
    /^On branch / {printf($3)}
    /^You are currently rebasing/ {printf("rebasing %s", $6)}
    /^Initial commit/ {printf(" (init)")}
    /^Untracked files/ {printf("|+")}
    /^Changes not staged / {printf("|?")}
    /^Changes to be committed/ {printf("|*")}
    /^Your branch is ahead of/ {printf("|^")}
    ')
    if [ -n "$STATUS" ]; then
        echo -ne " [$STATUS]"
    fi
}

PS1='\[\033[07;33;01m\]\n\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_status)\$ '
Enter fullscreen mode Exit fullscreen mode

That's all folks!

Top comments (5)

Collapse
 
mmphego profile image
Mpho Mphego

Thank you for this, I have just updated my bashrc from this:

git_branch () {
    # Get current Git branch
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
    }

git_last_update () {
    # Get last update on current Git branch
    git log 2> /dev/null | head -n 3 | grep ^Date | cut -f4- -d' ';
    }

export PS1="\[\033[0;32m\]\[\033[0m\033[0;38m\]\u\[\033[0;36m\]@\[\033[0;36m\]\h:\w\[\033[0;32m\] \$(git_branch) \$(git_last_update)\n\[\033[0;32m\]└─\[\033[0m\033[0;31m\] [\D{%F %T}] \$\[\033[0m\033[0;32m\] >>>\[\033[0m\] "

Enter fullscreen mode Exit fullscreen mode

to this:

_ip_add=$(ip addr | grep -w inet | gawk '{if (NR==2) {$0=$2; gsub(/\//," "); print $1;}}')
__git_status() {
    STATUS=$(git status 2>/dev/null |
    awk '
    /^On branch / {printf($3)}
    /^Changes not staged / {printf("|?Changes unstaged!")}
    /^Changes to be committed/ {printf("|*Uncommitted changes!")}
    /^Your branch is ahead of/ {printf("|^Push changes!")}
    ')
    if [ -n "$STATUS" ]; then
        echo -ne " ($STATUS) [$(git log 2> /dev/null | head -n 3 | grep ^Date | cut -f4- -d' ')]"
    fi
}
__ps1_startline="\[\033[0;32m\]\[\033[0m\033[0;38m\]\u\[\033[0;36m\]@\[\033[0;36m\]\h on ${_ip_add}:\w\[\033[0;32m\]"
__ps1_endline="\[\033[0;32m\]└─\[\033[0m\033[0;31m\] [\D{%F %T}] \$\[\033[0m\033[0;32m\] >>>\[\033[0m\] "
export PS1="${__ps1_startline} \$(__git_status)\n ${__ps1_endline}"



Enter fullscreen mode Exit fullscreen mode
Collapse
 
brandonwallace profile image
brandon_wallace

This is what I came up with. Thanks for this post Vlastimil.

blu='\[\033[01;34m\]'
pur='\[\033[01;35m\]'
blu='\[\033[01;36m\]'
wht='\[\033[01;37m\]'
ylw='\[\033[01;33m\]'
grn='\[\033[01;32m\]'
red='\[\033[01;31m\]'
blk='\[\033[01;30m\]'
clr='\[\033[01;00m\]'

# Run git branch if there is a .git directory present.
# Display current status of the git repository.
function git_branch() {
    if [ -d .git ] ; then
        GITSTATUS=$(git status | awk '
        /^Changes not staged/{printf("+")} 
        /^Untracked files/{printf("*")} 
        /^Changes not staged/{printf("?")} 
        /^Your branch is ahead of/{printf("^")}')
        printf "%s" "($(git branch 2> /dev/null | awk '/\*/{print $2}'))${GITSTATUS}";
    fi
}

PS1='${debian_chroot:+($debian_chroot)}'${blu}'$(git_branch)'${pur}' \W'${grn}' \$ '${clr}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
deltakapa profile image
deltakapa

Could you possibly share the color/syntax styling ? :P

Collapse
 
mmphego profile image
Mpho Mphego

Check my dotfiles: github.com/mmphego/dot-files

Collapse
 
rupankarghosh profile image
RupankarGhosh

Apart from git status,the colour and prompt styling in the bash looks so cool .