PS1 line with git current branch and colors

Here is my current PS1

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

How can I display the current branch in a different color?

  • Get SHA1 of latest remote commit
  • Is it possible to accept user input as part of a remote git post-receive hook?
  • Shell scripts with Git Bash 64-bit on Windows
  • Get sha of the last commit before pull
  • How can I get my git (msysgit on windows) post-commit script to invoke my python script as python rather than bash?
  • Git add submodules to super project from existing repos
  • What do “untracked directories” mean in Git?
  • How do I turn a git clone into a fork and then push to heroku from the fork?
  • How to find out what changes on a branch after merges from master?
  • Changing the name of the committer in Xcode
  • How visualice multiples repos and clone them (git instaweb, gitlist, gitlab)
  • Git - Find all descendant branches not merged in
  • 12 Solutions collect form web for “PS1 line with git current branch and colors”

    You can wrap the part that you want in colour with the following:

    \e[0;32m – sets colour (in this case, to green)

    \e[m – sets colour back to the default

    For example, this sets the prompt to the last token of the current path, in green, followed by $ in the default colour:

    export PS1='\e[0;32m\w\e[m $'

    Other colours are available too. Have a look at this article under colorization for a comprehensive list of alternatives.

    Here is, part by part (and no ruby):

    function color_my_prompt {
        local __user_and_host="\[\033[01;32m\]\u@\h"
        local __cur_location="\[\033[01;34m\]\w"
        local __git_branch_color="\[\033[31m\]"
        #local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
        local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
        local __prompt_tail="\[\033[35m\]$"
        local __last_color="\[\033[00m\]"
        export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "

    Looks like this (with my own terminal palette):

    colored prompt

    Also, see this and this arcticle

    Here is my PS1 line:

    \n\[\e[1;37m\]|-- \[\e[1;32m\]\u\[\e[0;39m\]@\[\e[1;36m\]\h\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(__git_ps1 " (%s)")\[\e[0;39m\] \[\e[1;37m\]--|\[\e[0;39m\]\n$

    alt text

    function pc {
      [ -d .git ] && git name-rev --name-only @
    PS1='\e];\s\a\n\e[33m\w \e[36m$(pc)\e[m\n$ '



    This is my PS1 solution

    Looks great on a mac with the Novel theme.
    Sorry but my indentation got munged a bit.
    Hack it till you like it.

    function we_are_in_git_work_tree {
        git rev-parse --is-inside-work-tree &> /dev/null
    function parse_git_branch {
        if we_are_in_git_work_tree
        local BR=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null)
        if [ "$BR" == HEAD ]
            local NM=$(git name-rev --name-only HEAD 2> /dev/null)
            if [ "$NM" != undefined ]
            then echo -n "@$NM"
            else git rev-parse --short HEAD 2> /dev/null
            echo -n $BR
    function parse_git_status {
        if we_are_in_git_work_tree
        local ST=$(git status --short 2> /dev/null)
        if [ -n "$ST" ]
        then echo -n " + "
        else echo -n " - "
    function pwd_depth_limit_2 {
        if [ "$PWD" = "$HOME" ]
        then echo -n "~"
        else pwd | sed -e "s|.*/\(.*/.*\)|\1|"
    # export all these for subshells
    export -f parse_git_branch parse_git_status we_are_in_git_work_tree pwd_depth_limit_2
    export PS1="$COLRED<$COLBROWN \$(pwd_depth_limit_2)$COLRED\$(parse_git_status)$COLBROWN\$(parse_git_branch) $COLRED>$COLCLEAR "
    export TERM="xterm-color"

    If you are checked out at a branch, you get the branch name.

    If you are in a just init’d git project, you just get ‘@’

    If you are headless, you get a nice human name relative to
    some branch or tag, with an ‘@’ preceding the name.

    If you are headless and not an ancestor of some branch or tag
    you just get the short SHA1.

    In addition, a red ‘-‘ signifies a clean work dir and index, and
    a red ‘+’ signifies the opposite.


    My uber-powerful multi-line Linux prompt!

    Put it either in your .bashrc or better: save it in /etc/bash-prompt and source it from your .bashrc.
    Using tput is supposed to be the right way to do colors.

       local last_cmd=$?
       local txtreset='$(tput sgr0)'
       local txtbold='$(tput bold)'
       local txtblack='$(tput setaf 0)'
       local txtred='$(tput setaf 1)'
       local txtgreen='$(tput setaf 2)'
       local txtyellow='$(tput setaf 3)'
       local txtblue='$(tput setaf 4)'
       local txtpurple='$(tput setaf 5)'
       local txtcyan='$(tput setaf 6)'
       local txtwhite='$(tput setaf 7)'
       # unicode "✗"
       local fancyx='\342\234\227'
       # unicode "✓"
       local checkmark='\342\234\223'
       # Line 1: Full date + full time (24h)
       # Line 2: current path
       PS1="\[$txtbold\]\[$txtwhite\]\n\D{%A %d %B %Y %H:%M:%S}\n\[$txtgreen\]\w\n"
       # User color: red for root, yellow for others
       if [[ $EUID == 0 ]]; then
       # Line 3: user@host
       # Line 4: a red "✗" or a green "✓" and the error number
       if [[ $last_cmd == 0 ]]; then
          PS1+="\[$txtgreen\]$checkmark \[$txtwhite\](0)"
          PS1+="\[$txtred\]$fancyx \[$txtwhite\]($last_cmd)"
       # Line 4: green git branch
       PS1+="\[$txtgreen\]$(__git_ps1 ' (%s)')\[$txtwhite\]"
       # Line 4: good old prompt, $ for user, # for root
       PS1+=" \\$ "

    Just invoke tput with the appropriate parameters. See the tput(1) and terminfo(5) man pages.

    Take a look at liquidprompt:

    Maybe a bit too heavy for your requirements, but you can switch features off by setting


    See the documentation on above page.

    Here is a windows/cygwin/bash solution.
    Add the following to your ~/.bashrc file.
    xxx is the location of your local Git repository.

        cat /cygdrive/c/xxx/.git/HEAD | sed 's+^ref: refs/heads/++'
    export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[36m\]\$(GetBranch) \[\e[33m\]\w \[\e[0m\] \n\$ "

    Here’s mine

    export PS1="\n\[\033[1;30m\][$$:$PPID - \j:\!\[\033[1;30m\]]\[\033[0;36m\] \T \
    \[\033[1;30m\][\[\033[1;34m\]\u@\H\[\033[1;30m\]:\[\033[0;37m\]${SSH_TTY:-o} \
    \[\033[0;32m\]+${SHLVL}\[\033[1;30m\]] \[\033[1;37m\]\w\[\033[0;37m\]\[\033[1;34m\]\$(__git_ps1 \" (%s)\") \[\033[0;37m\] \n\$ "

    for more complicated status of git you can use some larger script

    For my Mac with the Homebrew theme, this works really well. Fully debugged and very fast, and completely self-contained

    # Color prompt for git
    reset=$(tput sgr0)
    boldgreen=$(tput setaf 2)$(tput bold)
    cyan=$(tput sgr0)$(tput setaf 6)
    boldred=$(tput setaf 1)$(tput bold)
    boldwhite=$(tput setaf 7)$(tput bold)
    boldyellow=$(tput setaf 3)$(tput bold)
    alias branchname="git branch 2>/dev/null | sed -ne 's/^* \(.*\)/ ${PARENCLR}(${BRANCHCLR}\1${PARENCLR}\)/p'"
    PS1="\[$boldgreen\]\u\[$cyan\]::\[$boldred\]\h \[$cyan\]{\[$boldwhite\].../\W\[$cyan\]}\[$reset\]$GIT_STATUS\[$reset\]$PROMPT_CHAR "

    Here’s what it looks like:
    Mac + Homebrew + Color Git Prompt

    If you want to have the full path (or remove the …/), then just change the -W to -w (and remove the …/).

    Git Baby is a git and github fan, let's start git clone.