DEV Community

Siddharth Singh Tanwar
Siddharth Singh Tanwar

Posted on

Supercharge Your Terminal: Display Git Branch in Your Bash Prompt

Introduction

As developers, we spend a significant amount of time in our terminal, navigating through projects and managing Git repositories. One common pitfall is forgetting which Git branch we're currently working on, leading to potential mistakes or confusion. What if your terminal could always remind you of your current Git branch? In this post, we'll walk through a simple yet powerful customization of your bash prompt that will do just that.

What We'll Achieve

Before: username@hostname:$
After: username@hostname:/project (main)$

By the end of this tutorial, your bash prompt will automatically display your current Git branch when you're in a Git repository. This small change can significantly improve your workflow, reducing the need for frequent git branch commands and helping prevent accidental commits to the wrong branch.

Prerequisites

Before we begin, ensure you have:

  • Bash shell (default on most Linux distributions and macOS)
  • Git installed
  • Basic familiarity with editing configuration files

Step-by-Step Guide

a. Editing the .bashrc file
Open your .bashrc file in a text editor:

nano ~/.bashrc

b. Adding the parse_git_branch function
Add this function to your .bashrc:

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

c. Modifying the PS1 variable
Find your PS1 definition and modify it to include the Git branch information:

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[33m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi

d. Applying the changes
Save the file and run:

source ~/.bashrc

How It Works

The parse_git_branch function:

  • Runs git branch to get the list of branches
  • Uses sed to filter and format the output, showing only the current branch name in parentheses

The PS1 string:
\u@\h: Displays username@hostname
\w: Shows the current working directory
$(parse_git_branch): Calls our function to display the Git branch
Color codes (e.g., [\033[01;32m]) add visual distinction to different parts of the prompt

Customization Options

  • Change colors by modifying the color codes in PS1

  • Add Git status information by extending the parse_git_branch function

  • Include other Git information like the number of staged/unstaged files

Troubleshooting

  • If changes don't appear, ensure you've sourced .bashrc or opened a new terminal

  • Check for syntax errors in your .bashrc file

  • Verify that Git is installed and accessible from your PATH

Conclusion

This simple bash prompt customization can significantly enhance your development workflow. By always knowing which Git branch you're on, you can work more confidently and efficiently. I encourage you to customize your prompt further to suit your needs and workflow.

Further Resources

  • Bash manual: Advanced prompt customization

  • Git documentation: Working with branches

  • Bash scripting tutorials for more advanced prompt modifications

By implementing this customization, you're taking a small but significant step towards a more informative and efficient development environment.

Happy coding!

Top comments (2)

Collapse
 
shricodev profile image
Shrijal Acharya

Is there a specific reason you are sticking with bash? 🧐 There are great alternatives that offer these features as built-in.

Have you tried using Zsh or Fish?

Collapse
 
siddharth_singhtanwar_6a profile image
Siddharth Singh Tanwar

I have not. But I have seen folks use them online. I was interested in if I could replicate and have some of those features in bash too.