Back to Blog

Posted by

How to show the current Git branch in your terminal prompt

Hello there! In this article, I’ll show you how to enhance your Mac terminal by displaying the current Git branch in your prompt. This simple tweak can significantly improve your workflow by ensuring you're always aware of which branch you're working on.

If you need more convincing on why you should customize your Mac terminal, I listed some reasons here.

What is the terminal prompt?

Before we dive in, let's clarify what the terminal prompt is. The terminal prompt is the section of the terminal where you enter your commands. You can configure this to display additional information, such as the time a command was run or the current folder, as explained in another article. Today, we'll focus on adding the current Git branch to the prompt.

The Easy Way

Before we get into the manual setup, let me introduce you to BetterTerminal, a tool I created to help you customize your terminal without fiddling with configuration files.

BetterTerminal allows you to visually customize all aspects of your terminal, making the process straightforward.

If you prefer a simpler, more visual approach to terminal customization, check out the short demo video below:

Configuration Files

If you prefer to manually configure your terminal, the steps will vary depending on your shell type. First, determine your shell by running:

echo $SHELL

If the output is /bin/bash, you're using Bash. If it's /bin/zsh, you're using Zsh. Follow the instructions for your shell type below.

Bash Shells

To add the Git branch to the Terminal prompt in Bash shells, you need to modify your .bashrc file located in your home directory. To check if it exists, run:

ls -a ~

If .bashrc isn't listed, create it with:

touch ~/.bashrc

Understanding PS1

Before we add the Git branch, familiarize yourself with the PS1 variable, which defines your prompt. If you are not familiar with PS1, I encourage you to check out the previous article, which covers this topic in more depth.

Here's an example of a simple PS1 setup:

# Prompt Configuration
PS1="\n";
PS1+="[\\t] ";
PS1+="in ";
PS1+="\\W: ";
export PS1;

This configuration shows the current time and directory:

[15:42:21] in MyProject:

Note that we haven't added any colors to the above prompt. We can do that by defining color variables and adding them to the prompt, to create color contrasts that make it easier to identify each prompt segment.

Use our color lookup table to identify the colors that work best for you.

# Colors definition
color1=$(tput setaf 34)
color2=$(tput setaf 15)
color3=$(tput setaf 45)
color4=$(tput setaf 10)
reset=$(tput sgr0)

Then, incorporate these colors into the PS1 variable:

PS1="\n";
PS1+="\[${color1}\][\\t] ";
PS1+="\[${color2}\]in ";
PS1+="\[${color3}\]\\W: ";
PS1+="\[${reset}\]";
export PS1;

Again, our previous article provides a more detailed tutorial on this subject, which can be confusing at first. For now, lets continue with adding the git branch into the prompt.

Adding the git branch to terminal prompt in Bash shells

To display the current Git branch, add this function to your .bashrc file, above your PS1 variable:

# Parse git branch
parse_git_branch() {
  local branch_name
  branch_name=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  if [ -n "$branch_name" ]; then
    echo "($branch_name)"
  fi
}

This function fetches the current Git branch name if the current folder is part of a git repository. Next, modify your PS1 variable to include the Git branch:

# Prompt Configuration
PS1="\n";
PS1+="\[${color1}\][\\t] ";
PS1+="\[${color2}\]in ";
PS1+="\[${color3}\]\\W";
PS1+="\[${color4}\] \$(parse_git_branch)";
PS1+="\[${color2}\]: ";
PS1+="\[${reset}\]";
export PS1;

Save your .bashrc file and open a new terminal window to apply the changes. Your prompt should now look like this:

[15:42:21] in MyProject (main):

If you're not in a Git repository, the branch part will be omitted. Switch branches to see the prompt update:

git checkout -b prompt-test

The prompt will change to:

[15:42:21] in MyProject (prompt-test):

That's all there is to adding the current Git branch to your terminal prompt in Bash shells. It is a small but powerful enhancement.

If you are having issues with this configurations, here is another gentle reminder to check out BetterTerminal, which takes away the technical complexity of it all, by offering a visual editor that lets you focus on finding your perfect prompt composition.


Zsh Shells

To add the Git branch to the Terminal prompt in Zsh shells, you need to modify the .zshrc file located in your home directory. To check if it exists, run:

ls -a ~

If .zshrc is not listed, create it with

touch ~/.zshrc

Understanding the PROMPT variable

Before we add the Git branch, lets quickly recap how to define the PROMPT variable, which defines your zsh shell prompt. If you are looking for a deeper introduction to this topic, check out the previous article on terminal customization, which talks about this in more detail.

Here's an example of a simple prompt setup:

# Prompt Configuration
PROMPT="[%*] in %1~: ";
export PROMPT;

This configuration shows the current time and directory:

[15:42:21] in MyProject:

To add colors, define color variables and incorporate them into the PROMPT variable.

Use our color lookup table to identify the colors that work best for you.

# Colors and Prompt Setup
color1=$(tput setaf 34)
color2=$(tput setaf 15)
color3=$(tput setaf 45)
color4=$(tput setaf 10)
reset=$(tput sgr0)

PROMPT="%{$color1%}";
PROMPT+="%{${color1}%}[%*] ";
PROMPT+="%{${color2}%}in ";
PROMPT+="%{${color3}%}%1~";
PROMPT+="%{${color3}%}: ";
PROMPT+="%{$reset%}";
export PROMPT;

Again, our previous article provides a more detailed tutorial on the subject of adding colors to the prompt, which can be confusing at first. For now, lets continue with adding the git branch into the prompt.

Adding git branch to prompt in Zsh shells

To display the current Git branch, add this function to your .zshrc file, above your PROMPT variable:

# Parse git branch
parse_git_branch() {
  local branch_name
  branch_name=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  if [ -n "$branch_name" ]; then
    echo "($branch_name)"
  fi
}

This function fetches the current Git branch name if the current directory is part of a Git repository. Next, modify your PROMPT variable to include the Git branch and set the PROMPT_SUBST option:

# Prompt Configuration
setopt PROMPT_SUBST;
PROMPT="%{$color1%}";
PROMPT+="%{${color1}%}[%*] ";
PROMPT+="%{${color2}%}in ";
PROMPT+="%{${color3}%}%1~";
PROMPT+="%{${color4}%} \$(parse_git_branch)";
PROMPT+="%{${color2}%}: ";
PROMPT+="%{$reset%}";
export PROMPT;

Save your .zshrc file and open a new terminal window to apply the changes. Your prompt should now look like this:

[15:42:21] in MyProject (main):

If you're not in a Git repository, the branch part will be omitted. Switch branches to see the prompt update:

git checkout -b prompt-test

The prompt will change to:

[15:42:21] in MyProject (prompt-test):

Adding the current Git branch to your Zsh terminal prompt is a small yet powerful enhancement. It keeps you informed about your working branch, which reduces the risk of making changes in the wrong branch.

Whether you prefer manual configuration or using a tool like BetterTerminal, this customization can streamline your development workflow and improve efficiency.