DEV Community

Cover image for Include the Current Branch Name in Terminal Output
Nikola Perišić
Nikola Perišić

Posted on

2 1

Include the Current Branch Name in Terminal Output

If you're a developer or someone who works with Git regularly, you may have wished for a more organized and informative terminal prompt. Specifically, it can be incredibly useful to always know which Git branch you're on directly from the terminal. I’ll show you how to do it.


Before: The Default Terminal Prompt

Terminal Linux

As you can see, the prompt was simple. It displayed my username, hostname, and the current directory.

The Goal

Linux Terminal


Table of Contents

  1. Accessing the ~/.bashrc file
  2. Adding Git Branch Information
  3. Customizing the Prompt
  4. Final Step: Apply Changes
  5. Result
  6. Other shells (zsh, fish)

Step-by-Step: How I did it

To achieve this, I modified my terminal prompt by customizing the PS1 environment variable. The PS1 variable defines the prompt that appears in your terminal. By customizing it, you can add dynamic content, such as the current branch name.

Accessing the ~/.bashrc file

The PS1 variable is stored in the ~/.bashrc file, which is loaded whenever a new terminal session starts. To modify it, open the file in your preferred text editor:

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Adding Git Branch Information

I wanted to display the current Git branch only when I'm inside a Git repository. This can be done by using a shell function like parse_git_branch() which checks for the existence of a .git directory. If it exists, it extracts the current branch name.

Here's the function I used in my .bashrc:

parse_git_branch() {
  git branch 2>/dev/null | sed -n '/\* /s///p'
}
Enter fullscreen mode Exit fullscreen mode

This function checks the output of git branch, and extracts the branch name by looking for the line that starts with * (which represents the current branch).

Customizing the Prompt

Next, I customized my PS1 variable to include the current branch. I also added color for each part of the prompt. Here's the code I ended up using:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;91m\]\u@\h\[\033[00m\]:\[\033[01;35m\]\w\[\033[00m\]\[\033[01;92m\]$([[ -d .git ]] && echo " ($(parse_git_branch))")\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$([[ -d .git ]] && echo " ($(parse_git_branch))")\$ '
fi
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • \[\033[01;91m\]: Sets the username (\u) and hostname (\h) in bright red.
  • \[\033[01;35m\]: Sets the working directory (\w) in light purple.
  • \[\033[01;92m\]: Sets the Git branch name in bright green (which makes it stand out).

With this configuration, the terminal prompt will display the Git branch only when you're inside a Git repository, and it will be highlighted in the bright green color of your choice.

Final Step: Apply Changes

After modifying the ~/.bashrc file, make sure to apply the changes by running:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Result

Here’s the result after all the changes were made:

Linux Terminal


Other shells(zsh, fish)

For Zsh (Z Shell)

Include this in your ~/.zshrc file:

PROMPT='%F{red}%n@%m%f:%F{magenta}%~%f$([ -d .git ] && echo " (%F{green}$(git rev-parse --abbrev-ref HEAD)%f)") % '
Enter fullscreen mode Exit fullscreen mode

For Fish Shell

Include this in your ~/.config/fish/config.fish file:

function fish_prompt
    set_color red
    echo -n (whoami) "@" (hostname) " "
    set_color magenta
    echo -n (pwd) " "
    if test -d .git
        set_color green
        echo -n "(" (git rev-parse --abbrev-ref HEAD) ") "
    end
    set_color normal
    echo -n "> "
end
Enter fullscreen mode Exit fullscreen mode

I hope this was helpful. If you'd like to try out some other custom colors, feel free to leave a comment below, and I'll be happy to provide you with the code snippet! 💬

Example of the comment

Hey, Nikola! I want:

nickname/hostname color  #FF5733 (Bright Red)
path color  #8E44AD (Purple)
branch color  #2ECC71 (Green)
Enter fullscreen mode Exit fullscreen mode

I promise I will send you the solution within 6 hours. 😊

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay