Home Git Show git branch name in terminal with color

Show git branch name in terminal with color

319
git-branch

As a software developer using Git, we have to work on multiple Git branches. With default command prompt, it doesn’t display the name of the current branch. However, we can display it with color using the below code. As a result, it can remove the cumbersome to switch between branches and keep track of which branch we are currently on.

In this article, we will modify our terminal prompt to display the current Git branch using a simple Bash script. Please note that, the script will work on Linux and MacOS systems.

First of all, open your .bashrc file in a text editor or terminal. We can simply open this file in the terminal using the command nano ~/.bashrc and paste the following Bash function at the end of the file.

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

This simple function run the git branch command and extract the name of the current active branch. The sed command is used to remove any non-branch lines and the asterisk before the branch name.

As stated earlier, we will give some color to the branch name in the terminal. So, let’s define some color variables.

RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"

Now, we will modify the PS1 variable to include the active Git branch. You can choose to include the hostname or not, depending on your preference.

Without Hostname:

PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

With Hostname:

PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

The PS1 variable defines the format of the prompt. In this case, we’re using the variables we defined earlier to color different parts of the prompt. The \u and \h variables represent the current user and hostname, respectively. The \w variable represents the current working directory. Finally, we’re calling the parse_git_branch function to append the current Git branch to the prompt.

Save your changes to the file, and either open a new terminal window or run source ~/.bashrc or source ~/.bash_profile to apply the changes to your current terminal session. So, the final code looks like below:

# Add in ~/.bashrc or ~/.bash_profile
function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
 
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"

# without host
PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
# with host
# PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Now, when you navigate to a Git repository directory, you’ll see the current Git branch displayed in your terminal prompt, making it easy to keep track of which branch you’re on.

In conclusion, displaying the Git branch in your terminal prompt is a simple but powerful way to stay organized while working on multiple branches. By using the Bash script we’ve provided in this post, you can easily modify your terminal prompt to display the current Git branch on both macOS and Linux systems.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.