#Bash

How to Show Git Branch Name in Shell Console

Sometimes while working on multiple branches in a project one can forget to switch the branch before doing some changes, this is specially true for those who enjoy using console over UI tools.

A neat trick to avoid this is to change the shell configuration to show the branch name next to the working directory. To do this we will need to change the shell prompt configuration stored in PS1.

Before jumping into the details, for those who are not interested in knowing the details, you just need to add the following block to your .bashrc or .bash_profile in your home directory:

get_branch_name() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

branch_config="0;32m"
path_config="0;34m"
username_config="1;32m"
simple_white="00m"

export PS1="\[\e[${username_config}\]\u@:\[\e[${path_config}\]\w\[\e[${branch_config}\]\$(get_branch_name)\[\e[${simple_white}\]\$ "

The prompt will look like this:

Explanation

For the sake of simplicity, this section will focus on the basics used in displaying the branch name and won’t go into all the details of prompt command and PS1-PS4.

PS1 is the environment variable stores the default interactive prompt format of your shell. By changing the format and text configuration we can customize the prompt to your preference or your need. The format used in the code block above can be simplified to a list of smaller pieces of text format followed by the text.

Text Format

The text format determines the text style, color and background of the text following the configuration. it is written in PS1 like this \[\e[d;ddm]

  • [\e[ - Indicates the beginning of the text config
  • dd;dd - The first part is the text style while the second part is the text color
  • m] - Indicated the ending of the text config

Text style options:

  • 0 = none
  • 1 = bold
  • 4 = underscore
  • 5 = blink
  • 7 = reverse
  • 8 = concealed

Text color options:

  • 30=black
  • 31=red
  • 32=green
  • 33=yellow
  • 34=blue
  • 35=magenta
  • 36=cyan
  • 37=white

Text

The text value is what going to show in the prompt, the text will take the style defined right before it. For this configuration: \[\e[4;34m]example#:

Will appear in the prompt like this:

In addition to hard coded text, PS1 has predefined codes to help customize the prompt further, here are some of the popular ones:

  • \H the hostname
  • \t the current time in 24-hour HH:MM:SS format
  • \T the current time in 12-hour HH:MM:SS format
  • \u the username of the current user
  • \w the current working directory, with $HOME abbreviated with a tilde. Note if the current directory is under the user home directory then the path will start with ~/
  • \W the base name of the current working directory, with $HOME abbreviated with a tilde

Customize PS1 to show username@:directory (branch name)$ in the prompt:

The username@directory/path (branch name)$ consist of four parts: username, directory path, branch name and the user input. We will go over the configuration of each them one by one before putting them all together.

1- username

for the username part, we are going to show it in the prompt at the beginning with green bold text followed by “@:”
The configuration for that is: “\[\e[1;32m]\u@:” 1;32m means bold green while \u will print the current username and finally@: is hard coded text.

2- Directory path

we want the directory path to appear in normal blue text. The configuration for this is: “\[\e[0;34m]\w”

3- Branch name

In order to get the branch name, the prompt will need to check the git branch in the current directory every time. So to achieve this, we will need to create a function first.

get_branch_name() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

The git branch will get the branches names including the selected one and will throw an error if the directory is not a git project. 2> /dev/null will redirect stderr so it won’t appear in the prompt. Finally the sed command will use a regex pattern to retrieve branch name and print it in the format “(branch name)”

Now we want the function to be called in runtime when the prompt is being printed and have the text in green color, to do it we will use the following configuration: “\[\e[0;32m]$(get_branch_name)”

4- User input

The branch name section configuration will affect the user input, meaning the input will be in green color, we want the user input to remain a simple white text therefore we will have to add one more configuration for the input. the configuration for white text is “[\e[00m]$ “. 00m will give us the simple white style while $ will print $ for regular users and # for root user.

Conclusion

The article went through some of the basics of the prompt customization and how to show the branch name. The Linux shell is a powerful tool with a lot of uses and customization options that many doesn’t make use of, my hope is that the article will help in understanding this great tool more to improve working experience of those who uses it.