How to display the current Git branch in your Terminal
Have you ever wondered how a tool like Git Bash can dynamically display the current Git branch of a software project in the terminal? Have you ever asked yourself if you can have this functionality in your own shell?
Have you ever wondered how a tool like Git Bash can dynamically display the current Git branch of a software project in the terminal? Have you ever asked yourself if you can have this functionality in your own shell?

I did, last week, and I set out to investigate.
Turns out, there’s far less magic involved than expected!
How is the Bash prompt configured?
To learn how you can mimic this configuration in your preferred shell, I am using my Bash shell as an example.
Every user can create/modify a personal Bash shell (/bin/bash) configuration. This configuration is stored in the file .bashrc, typically found at /home/myuser/.bashrc.
I am not going to go over all configuration possibilities in
.bashrc, but only zoom in on the relevant section for the purpose of displaying the current Git branch.
The following section was taken from a default Debian 12 installation:
43 # uncomment for a colored prompt, if the terminal has the capability; turned
44 # off by default to not distract the user: the focus in a terminal window
45 # should be on the output of commands, not on the prompt
46 #force_color_prompt=yes
47
48 if [ -n "$force_color_prompt" ]; then
49 if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
50 # We have color support; assume it's compliant with Ecma-48
51 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
52 # a case would tend to support setf rather than setaf.)
53 color_prompt=yes
54 else
55 color_prompt=
56 fi
57 fi
58
59 if [ "$color_prompt" = yes ]; then
60 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
61 else
62 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
63 fi
It is the declaration of PS1 at line 60 that results in a colored terminal prompt.
Let’s break down this line:
# this is the builtin variable for prompt control
PS1=
# important, the variable's value is encapsulated in single quotes
'
# it means something, but I forgot
${debian_chroot:+($debian_chroot)}
# display user (\u) @ host (\h) in the color green (\[\033[01;32m\]), then switch back to white (\[\033[00m\])
\[\033[01;32m\]\u@\h\[\033[00m\]:
# display the current working directory (\w) in the color blue
\[\033[01;34m\]\w\[\033[00m\]\$
# don't forget the closing quote
'
The result is:

Now we understand how the prompt is declared, we can try to customize our prompt. One thing that is important to know, is that the prompt gets updated every time you enter a command. Consider this example:

When you change current directory, the prompt reflects this. This happens because \w is a reference to something like the pwd function, which purpose is to display the current working directory.
We are going to use this technique to create a small function that displays the current Git branch if you happen to be in a directory that is initialized as a Git repository.
Create the Parse Git Branch function
First of all open your .bashrc file, and navigate to the section right above the declaration of PS1. Create a few extra newlines for a function called parse_git_branch().
For now, just add a simple echo statement, so you can first test with calling the function from PS1, before getting knee deep in to branch parsing logic :-).
54 parse_git_branch() {
55
56 echo "current git branch"
57 }
58
59 if [ "$color_prompt" = yes ]; then
60 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
61 else
62 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
63 fi
To call the function from PS1, you will have to do something like this:
59 if [ "$color_prompt" = yes ]; then
60 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\$ '
61
62 ## abbreviated for clarity
Following this example your bash prompt would now be something like this:

Now let’s make this useful by having it display either the current Git branch, or nothing (when the directory isn’t a Git repository). Modify the parse_git_branch() function:
parse_git_branch() {
current_branch=$(git branch --show-current 2> /dev/null)
if [ ! -z "$current_branch" ]; then
echo "[$current_branch]"
fi
}
Now open a new Terminal window (or execute bash) to see the final result:

Add some color
Cool huh!? But what about adding some color, to make your prompt stand out!
To figure out how you can declare colors in a Bash prompt, browse to shellhacks.com/bash-colors, for a handy overview.
For a simple color scheme, take a look at this table:

Then open .bashrc again to modify PS1
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\$ '
Say we want user and host in “Light Cyan”, the current working directory in “Light Green” and our Git branch in “Light Red”
First, modify user and host section.
# The original 01;32m
PS1='<...>\[\033[01;32m\]\u@\h\[\033[00m\]:<...> '
# Becomes 01;36m
PS1='<...>\[\033[01;36m\]\u@\h\[\033[00m\]:<...> '
Then, continue with the current working directory.
# The original 01;34m
PS1='<...>:\[\033[01;34m\]\w\[\033[00m\]<...> '
# Becomes 01;32m
PS1='<...>:\[\033[01;34m\]\w\[\033[00m\]<...> '
Finally, add some color to the Git branch
# The original without color
PS1='<...>$(parse_git_branch)\$ '
# Becomes 01;31m
PS1='<...>\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
The complete line will be:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;36m\]\u@\h\[\033[00m\]:\[\033[01;32m\]\w\[\033[00m\]\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
Try it out by executing bash or opening a new Terminal window. Now your prompt should look something like this:

I hope you enjoyed this little tutorial, have a great day!