blog.devgenius.io 3 min read Original Source

How to Customize the Git Bash Shell Prompt

Git for Windows provides lots of native and lightweight tools, as well as a Bash emulation that brings shell experience to users. The default Bash terminal looks like the following.

The terminal window title contains the full path of the current directory, and the prompt displays the current username, the host name, as well as the path of the current working directory. If the current working directory is a Git repository, the prompt will also show the Git branch name. We should appreciate that the terminal is very informative.

However, sometimes we don’t want to show the full path of the current directory or our username and host name. For example, I don’t want to expose my username and my machine name in the screenshots in my blog posts. Therefore, I configure the Git Bash terminal window to display a static string “git@local” as the username and host name, and only display the basename of the current working directory. In this way, I can hide some personal information. My terminal window looks like the following screenshot:

According to an article by Alan P. Barber (link), customizing the Bash prompt is actually very easy.

First, we need to identify the installation path of Git for Windows, and find the profile definition folder by executing the following commands in a Git Bash terminal.

cd /etc/profile.d/
explorer .
cd /etc/profile.d/
explorer .

The commands above will open a folder in the file explorer, as shown in the screenshot below.

We are looking for the git-prompt.sh file in the profile.d folder. The git-prompt.sh file contains the configurations for the title of the Git Bash terminal and the Bash prompt string. We can change the colors of the prompt, and modify the prompt content based on our needs. The default git-prompt.sh file has the following content.

[Embedded content: 8af68578e718cd42d4d6a66cc33ef307]

The git-prompt.sh file has detailed comments, which greatly helps our customization. We can modify this file to customize our Git Bash terminal, or we can keep this file as it is and save our configurations to our home directory (see lines 8 to 11). I prefer the latter way because I can keep the original git-prompt.sh file pristine. Thus, I create a folder ~/.config/git, and in the folder I create a file ~/.config/git/git-prompt.sh which will take precedence over the default git-prompt.sh file. The following code snippet shows the content of my configuration file.

[Embedded content: a0048bff673e3604b49031ee27f6b8b1]

With this file, my terminal prompt looks like this.

We mainly need to customize the PS1 string (primary prompt). The PS1 string uses a set of Bash prompt backslash-escaped special characters to display dynamic content. For example, u represents the current user’s username, w (lowercase) is the path of the current working directory, W (uppercase) is the basename of the current working directory, and so on. The following table shows all the escape characters that can be used to configure the prompt.

[Embedded content: 44c7ab5ea1d4a7f310fb82f490798d8f]

The construction of the PS1 string also uses some ANSI color escape codes. The color escape code starts with [ and ends with ]. The color format is 33[32m, where 33 represents the ASCII escape character, and the code 32 represents foreground color green. We can use the following color codes in the prompt string.

[Embedded content: b8d1b93c055b224a220337c34c1554ce]

Alright. I think that’s all for the customization of our Git for Windows Bash prompt. Thanks for reading.