February 18, 2025

How to Display Git Branch Name in Linux Terminal

February 18, 2025

How to Display Git Branch Name in Linux Terminal

Ever switched to the wrong Git branch by accident? Yeah, me too. Here's a simple way to always know which branch you're on by adding it right to your terminal prompt. No more surprises!

Introduction

Hi Folks!

I am going to show how to display current active git branch on the linux terminal.

Let’s be honest – we’ve all been there. You’re deep into coding, juggling multiple Git branches, and suddenly realize you have been working on the wrong branch for the last hour. Ouch.

What if your terminal could always tell you which branch you’re on?
No more typing git branch every few minutes or making accidental commits to the wrong branch.

Today, I’ll show you a neat trick to add your Git branch name right to your terminal prompt.
It’s a game-changer for your daily workflow!

Prerequisites

  • Linux operating system
  • Git installed on your system
  • Basic familiarity with terminal commands
  • Text editor (VS Code, Nano, or Vim)

Step-by-Step Guide

1. Open the Bash Configuration File

First, we need to modify the .bashrc file, which controls your bash shell configuration:

nano ~/.bashrc

2. Add the Git Branch Function

Add the following code at the end of your .bashrc file:

# Display the git branch name
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\$ "

3. Apply the Changes

After saving the file, reload your bash configuration:

source ~/.bashrc

4. Output

Display current active branch

Troubleshooting

1. Changes Not Visible:

  • Make sure you ran source ~/.bashrc
  • Verify the code was added correctly to .bashrc

2. Branch Name Not Showing:

  • Confirm you’re in a Git repository
  • Check if Git is installed: git --version

Conclusion

Displaying the Git branch name in your terminal is a simple yet powerful modification that can significantly improve your development workflow.

This customization helps prevent mistakes when working with multiple branches and makes your terminal more informative and user-friendly.

The color-coded prompt provides clear visual feedback about your current location and Git status, making it easier to navigate between projects and branches. With these changes, you’ll spend less time checking your Git status and more time focusing on your development tasks.

Happy coding! And hey, if you found this helpful, share it with your fellow developers. We’ve all been bitten by the wrong-branch bug at least once! 😉

Thank you for reading this article.

Share on:
Share on:
LinkedIn
Email
Facebook
Reddit
Twitter
WhatsApp
Skype
Print

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

Recent posts

LogicRays Blogs

Read other latest blogs on technology, trending, Web & Mobile App, E-Commerce related etc.
We recommend
Featured posts

Table of Contents