Display Your Current Git Branch in Ubuntu Terminal Prompt

The Ubuntu terminal doesn’t inherently display the currently active Git branch, leaving you to manually run git branch to check. However, here’s the good news: you can modify your terminal settings to automatically show the active Git branch. In this guide, we’ll walk you through the process step by step.

Introduction

Many developers find it helpful to have their current Git branch displayed in their terminal prompt. This feature saves time and provides valuable information about the state of your project. Instead of typing git branch each time, you can customize your terminal to show the branch name directly in the prompt.

Summary of Steps

Here are the steps you need to follow to display your current Git branch in the Ubuntu terminal prompt:

  1. Open the ~/.bashrc file in your home folder using a text editor with root permissions.
  2. Copy and paste the provided code snippet into the ~/.bashrc file.
  3. Save the file.
  4. Restart the terminal or open a new tab to see the changes take effect.

Detailed Steps with Code Examples

Step 1: Edit ~/.bashrc File

To begin, open the ~/.bashrc file located in your home folder using a text editor with root permissions. You can use a command like sudo nano ~/.bashrc to open it.

Step 2: Copy and Paste the Code

Once you have the ~/.bashrc file open, copy and paste the following code snippet into it:

# Author: Shiva Bhusal
# GitHub: shivabhusal
# Stack Overflow: users/3437900/illusionist
##########################################


# Usages:
# - Create a file called ~/.promptrc
# - Add the following line in ~/.bashrc:
#   `source ~/.promptrc`
#   # This will include the source of the file


# Content of the file ~/.promptrc

# @returns {String}
#  Example: master
function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
NO_COLOR="\[\033[0m\]"

RUBY_VERSION="\$(~/.rvm/bin/rvm-prompt)"

# \w --> working directory
# \u --> current user
# \h --> Computer name

export PS1="$GREEN\u:$BLUE\w$YELLOW (${RUBY_VERSION} : $(parse_git_branch)) $NO_COLOR$ "

Step 3: Save the File

After pasting the code snippet, save the ~/.bashrc file.

Step 4: Restart the Terminal

To apply the changes, you need to either restart your terminal or open a new terminal tab. Once you do that, your terminal prompt will display the active Git branch alongside your username and working directory.

Alternative Using - ZSH/OhMyZsh

Step 1: Install Oh My Zsh

If you haven’t already installed Oh My Zsh, you can do so by running the following command:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

To activate a theme, specify the theme name in your ~/.zshrc file by setting ZSH_THEME before sourcing Oh My Zsh. For instance, you can set it like this:

ZSH_THEME="agnoster"

If you prefer not to use any theme, simply set ZSH_THEME to an empty value like this: ZSH_THEME="".

ohmyzsh prompt

Conclusion

With these simple steps, you can enhance your Ubuntu terminal’s functionality by showing the current Git branch in the prompt. This small modification can greatly improve your workflow and productivity when working with Git repositories. Enjoy your newfound convenience!