Customizing Git Bash in Windows
Tags: bash, git, tutorial
Published: 2021-05-21 16:07:23


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht25lmlvuzj46ltnd6br.png)

so let's dive in!

## Finding and Editing git-prompt

Open your git bash terminal and type this commands:

```bash
cd /etc/profile.d/
explorer .
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r50gpsse84pkx3rsuxnq.png)

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

```sh
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
```

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

```sh
	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
```

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:

```sh
PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'\u@\h '             # user@host<space>
```

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

```sh
Black        30  
Red          31     
Green        32     
Brown/Orange 33     
Blue         34     
Purple       35     
Cyan         36
Light Gray   37 
```

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
```

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

```sh
PS1="$PS1"'\[\033[1;36m\]'       # change to bold 1() and cyan (36)
```

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

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

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

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

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:

```sh
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
```

### Useful Links

- [customize git bash](https://blog.devgenius.io/how-to-customize-the-git-bash-shell-prompt-336f6aefcf3f)