1. Introduction

A branch in Git is a separate line of development within a repository. In particular, it enables developers to work on different parts of the code without interfering with one another’s work or altering the current stable core of the source code.

In this tutorial, we’ll learn how to show the Git branch with colors in Bash in three different ways.

2. The Bash Prompt

The default prompt in Bash usually contains the current username, hostname, and working directory. This main prompt is defined by the environment variable PS1, i.e., Prompt String 1.

Let’s check the contents of PS1 with the echo command:

user@machine-name ~$ echo $PS1
\u@\h \W\$

From the output, we can see that escaped characters encode values to the prompt:

  • \u denotes the username of the current user
  • \h encodes the hostname of the local machine minus the trailing domain name
  • \W shows the last part of the current working directory name
  • \$ displays the $ character or a # if we have superuser privileges

Now, let’s modify the PS1 environmental variable to show the Git branch with colors in Bash.

3. The __git_ps1 Function

The __git_ps1 function is provided by the Git version control system package and we can employ it to create a colored prompt for Git branches.

It’s used in combination with the PS1 variable to customize the prompt:

Colored_Git_Branch

To do this, let’s append the necessary code to our ~/.bashrc file:

export PS1='\u@\h \[\e[32m\]\w \[\e[91m\]$(__git_ps1)\[\e[00m\]$ '

In essence, we just include __git_ps1 in a command substitution structure within PS1. Importantly, the color codes are enclosed in \[ and \] to indicate that they are non-printing characters.

Further, to apply the change in the ~/.bashrc file, let’s reload it via the source builtin:

$ source ~/.bashrc

Thus, we update the system with our new configuration. However, we can also close and reopen the terminal for the change to take effect.

Notably, this modification applies exclusively when working in a Git repository.

4. The parse_git_branch Function

Alternatively, we can use the parse_git_branch function to modify the Bash prompt to show the Git branch with colors. To do this, let’s append the following function to our ~/.bashrc file:

# enable colored prompt for git branches
# using parse_git_branch
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

Now, let’s break down the code:

  • git branch lists all the Git branches
  • 2> /dev/null redirects any error messages if we’re not in a Git repository
  • sed sifts out the current branch

Further, we alter the PS1 environmental variable to contain configurations for our Git prompt:

# modify the PS1 variable to include the parse_git_branch
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;33m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi

Now, let’s break down the code:

  • if [ “$color_prompt” = yes ]; then is a conditional statement that checks whether the color_prompt variable is set to yes
  • ${debian_chroot:+($debian_chroot)} includes the value of the debian_chroot variable inside parentheses if it’s not empty (often used in Debian-based systems to indicate the presence of a chroot environment)
  • $(parse_git_branch) executes the parse_git_branch function and includes its output (the current Git branch name) in the prompt

Lastly, the else block sets the PS1 variable without the color codes but still includes the same information about the user, hostname, working directory, and Git branch, if applicable.

5. The –show-current Flag

Similarly, we can employ the git branch –show-current command in place of  __git_ps1. The git branch command along with the –show-current flag can colorize the prompt for the current Git branch in Bash. To do this, let’s reset the PS1 variable by altering the value of PS1 in the ~/.bashrc file:

export PS1='\u@\h \[\e[32m\]\w \[\e[91m\]$(git branch --show-current)\[\e[00m\]$ '

Notably, the $(git branch –show-current) shows the current Git branch when in a Git repository. Again, to apply the change, we can log out and log in again or source the configuration file.

In fact, this code snippet sets the prompt to display the username, hostname, current working directory, and the current Git branch (if any) with green and red colors.

6. Conclusion

In this article, we learned how to show the current Git branch with colors in Bash using three different methods.

First, we briefly discussed the role of PS1 as an environmental variable. Then, we saw how to use __git_ps1, a built-in function in the Bash shell.

Further, we explored the use of the parse_git_branch function and a conditional statement.

Finally, we used the git branch command along with the –show-current flag as an alternative to __git_ps1.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.