DEV Community

Guilherme_Konan
Guilherme_Konan

Posted on

Customizing Git Bash in Windows

Hello guys, this is gonna be a small post about git bash customization, so by the end of it you will know how to change some aspects of your git terminal, for example making it like mine:

Alt Text

so let's dive in!

Finding and Editing git-prompt

Open your git bash terminal and type this commands:

cd /etc/profile.d/
explorer .
Enter fullscreen mode Exit fullscreen mode

The first command is to change your current directory to the /etc/profile.d/ that is where the file we need to edit is. The other command just open the explorer in the current directory, so you will se something like this:

Alt Text

Here is our git-prompt.sh. Now we can open it and see what we have there.

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
Enter fullscreen mode Exit fullscreen mode

Here we have a lot of different things, but let's focus on the important part, this one:

    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
Enter fullscreen mode Exit fullscreen mode

Here is where we are gonna customize it. The nice thing about git bash, is that there are already some pretty comments that help us understand what each thing does.

To customize it we are gonna need to understand two things: The special backslash characters and the ANSI color escape codes.

The first one is pretty simple, there are some of them and they each represent something that can be seeing in our terminal, here are some of them:

\u => username

\h => hostname until the first '.'

\w => current working directory

So for example the line PS1="$PS1"'\u@\h' Would be username@hostname.

The last thing is the color codes. Those are used before the line we want to color it, so:

PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'\u@\h '             # user@host<space>
Enter fullscreen mode Exit fullscreen mode

Here we are changing the username@hostname to green. There are a few color codes we can use in bash such as:

Black        30  
Red          31     
Green        32     
Brown/Orange 33     
Blue         34     
Purple       35     
Cyan         36
Light Gray   37 
Enter fullscreen mode Exit fullscreen mode

There are some special codes that change the style of the text too:

Normal        0
Bold text     1
Faint text    2
Italic        3
Underlined    4
Enter fullscreen mode Exit fullscreen mode

So we can make some pretty cool customization combining those together, here is an example:

PS1="$PS1"'\[\033[1;36m\]'       # change to bold 1() and cyan (36)
Enter fullscreen mode Exit fullscreen mode

There are some other lines that you can customize. Basically every line that is changing PS1 represents a customization for some part, and all of them have good comments to guide you. I only focused in the first ones to be easier to understand, since you are focusing in only a couple lines, instead of the entire file. But if you prefer that I point out them, they are these lines here:

This one changes the part that indicates in which branch you are

PS1="$PS1"'\[\033[1;33m\]'  # change color to bold yellow
PS1="$PS1"'`__git_ps1`'   # bash function
Enter fullscreen mode Exit fullscreen mode

And this ones defines the second line, where you write the commands (in case is only the "$")

PS1="$PS1"'\[\033[1;31m\]'     # change color to bold red
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'$ '                 # prompt: always $
Enter fullscreen mode Exit fullscreen mode

And that's it, with this knowledge you can make some more customizations in your git bash :). If you liked how mine feel free to get the code for you here:

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;Terminal Dir:$PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'\[\033[1;35m\]'       # change to bold purple
    PS1="$PS1"'GMkonan '             # user@host<space>
    PS1="$PS1"'\[\033[1;32m\]'       # change to bold green
    PS1="$PS1"'in '          # show MSYSTEM
    PS1="$PS1"'\[\033[1;36m\]'       # change to bold cyan
    PS1="$PS1"'\W'                 # directory without the path
    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[1;33m\]'  # change color to bold yellow
            PS1="$PS1"'`__git_ps1`'   # bash function
        fi
    fi
    PS1="$PS1"'\[\033[1;31m\]'        # change color to bold red
    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

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

Useful Links

Top comments (1)

Collapse
 
uchitesting profile image
UchiTesting • Edited

Thank you. I did it in the past but this is the kind of operation you hardly ever do and therefore forget.
I like and bookmark it for it will be of use later for sure.

Cheers.

EDIT:

And let add my personal aliases as backup.

aliases.sh

alias glog='git log --oneline'
alias glog10='git log --oneline -n 10'
alias gitgraph='git log --oneline --graph'
alias gstatus='git status -u'
# Will ask for the passphrase everytime as I want.
alias gaddssh='eval $(ssh-agent -s);ssh-add /C/Users/UchiTesting/.ssh/id_rsa'
Enter fullscreen mode Exit fullscreen mode