'How to Shorten Git Bash Prompt (Windows)

How do I shorten my git bash prompt from something like this

Malik@LAPTOP-7R9912OI MINGW64 ~/Desktop/test
$

to something like this

Malik@test$

I am using git bash on windows with git version 2.21.0 (26-02-2019)



Solution 1:[1]

In Git Bash:

cd ~
notepad .bashrc

In notepad, add the line PS1="foobar>" (replace foobar> with whatever text you want)

After saving ~/.bashrc, in Git Bash, run the command:

source ~/.bashrc

You may find this online .bashrc generator useful to experiment with to find a prompt you like.

Solution 2:[2]

An alternative answer is to go to C:\Program Files\Git\etc\profile.d and open the git-prompt.sh file. It contains the default configuration/prompt for Git Bash.

if test -f /etc/profile.d/git-sdk.sh
then
    TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
    TITLEPREFIX=$MSYSTEM
fi

if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'\[\033[32m\]'       # change to green
    # PS1="$PS1"'\u@\h '             # user@host<space>
    # PS1="$PS1"'\[\033[35m\]'       # change to purple
    # PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
    # PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
    PS1="$PS1"'\W'                 # current working directory
    if test -z "$WINELOADERNOEXEC"
    then
        GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
        COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
        COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
        COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
        if test -f "$COMPLETION_PATH/git-prompt.sh"
        then
            . "$COMPLETION_PATH/git-completion.bash"
            . "$COMPLETION_PATH/git-prompt.sh"
            PS1="$PS1"'\[\033[36m\]'  # change color to cyan
            PS1="$PS1"'`__git_ps1`'   # bash function
        fi
    fi
    PS1="$PS1"'\[\033[0m\]'        # change color
    # PS1="$PS1"'\n'                 # new line
    PS1="$PS1"' $ '                 # prompt: always $
fi

MSYS2_PS1="$PS1"               # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
    for c in "$HOME"/bash_completion.d/*.bash
    do
        # Handle absence of any scripts (or the folder) gracefully
        test ! -f "$c" ||
        . "$c"
    done
fi

In my configuration, I commented out the user@host<space>, the MINGW64 and changed the working directory to its basename by changing \w to \W.

Solution 3:[3]

I'd like to offer some tweaks which are based off of Krizza's answer. The prompt file can be overwritten anytime you update your Git For Windows installation so it's best to make a copy of it. Fortunately the file has a built in plan for this:

First make a copy of the default git-prompt.sh file and put it in your own personal config location. In my case the command is:

cp /c/Program\ Files/Git/etc/profile.d/git-prompt.sh ~/.config/git/

Do not skip this step! Now make sure to update your copy of the file by removing everything except what's inside of the larger else clause where PS1 is set. (At the time of this writing I'm keeping only lines 12-36.) Note, if you skip this step Git Bash will get stuck in an infinite loop and exit.

Now you can edit your copy of the file however you please. My personal preference is to comment out displaying the user and host, and the MSYSTEM, and instead displaying the current time of day, like this:

# PS1="$PS1"'\[\033[32m\]'       # change to green
# PS1="$PS1"'\u@\h '             # user@host<space>
PS1="$PS1"'\[\033[35m\]'       # change to purple
PS1="$PS1"'\D{%H:%M:%S} '      # show current time
# PS1="$PS1"'$MSYSTEM '          # show MSYSTEM

I personally find the time of day useful so when I look back at a prompt later I can see the time I ran the preceding command, e.g. git fetch, etc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Govind Parmar
Solution 2 Krizza
Solution 3 TTT