Speeding Up Git Bash in Windows

Dec 12, 2024

The Git Bash prompt in Windows is shockingly slow. I found this gist that speeds it up considerably. Simply place this in your user's .bash_profile file.

# Very very fast __git_ps1 implementation 
# 100% pure Bash (no forking) function to determine the name of the current git branch
# Modified from: https://gist.github.com/Ragnoroct/c4c3bf37913afb9469d8fc8cffea5b2f
# Which was inspired by https://gist.github.com/wolever/6525437
function __fastgit_ps1 () {
    branch=""

    local headfile head branch
    local dir="$PWD"

    while [ -n "$dir" ]; do
        if [ -e "$dir/.git/HEAD" ]; then
            headfile="$dir/.git/HEAD"
            break
        fi
        dir="${dir%/*}"
    done

    if [ -e "$headfile" ]; then
        read -r head < "$headfile" || return
        case "$head" in
            ref:*) branch="${head##*/}" ;;
            "") branch="" ;;
            *) branch="${head:0:7}" ;;  #Detached head. You can change the format for this too.
        esac
    fi

    if [ ! -z "$branch" ]; then
        branch="($branch)"
    fi

    # Edit to suit your needs. Note the branch will be wrapped in parenthesis if it's set. Completely empty otherwise.
    export PS1="\[\e]0;\W\a\]\n\[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \[\033[36m\]$branch\[\033[0m\]\n\$ "
}
export PROMPT_COMMAND=__fastgit_ps1

No comments (yet!)

Leave a Comment

Ignore this field:
Never displayed
Leave this blank:
Optional; will not be indexed
Ignore this field:
Both Markdown and a limited set of HTML tags are supported
Leave this empty: