Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
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.
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:
Now, let’s modify the PS1 environmental variable to show the Git branch with colors in Bash.
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:
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.
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:
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:
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.
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.
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.