funwithlinux blog

How to Show Current Git Branch with Coloring in Linux Terminal (Like Git Bash on Windows)

If you’ve used Git Bash on Windows, you’ve likely noticed its handy feature: the terminal prompt automatically displays the current Git branch you’re working in, often with color coding for clarity. This small but powerful detail saves time and prevents mistakes—no more running git branch repeatedly to check where you are!

Linux terminals, by default, don’t show this information. But with a few tweaks to your shell configuration, you can replicate (and even enhance) this functionality. In this guide, we’ll walk through how to configure your Linux terminal to display the current Git branch with custom coloring, step by step.

2026-01

Table of Contents#

  1. Prerequisites
  2. Understanding the Linux Shell Prompt
  3. Step 1: Identify Your Shell
  4. Step 2: Create a Git Branch Detection Function
  5. Step 3: Modify the Prompt to Show the Git Branch
  6. Step 4: Add Color to the Git Branch (and Prompt)
  7. Step 5: Test Your New Prompt
  8. Troubleshooting Common Issues
  9. Adapting for Zsh (Optional)
  10. Conclusion
  11. References

Prerequisites#

Before starting, ensure you have the following:

  • Git installed: Run git --version in the terminal. If not installed, use your package manager (e.g., sudo apt install git for Debian/Ubuntu, sudo dnf install git for Fedora, or sudo pacman -S git for Arch).
  • A Linux terminal: Most default terminals (GNOME Terminal, Konsole, Tilix, etc.) work. Ensure it supports ANSI color codes (nearly all modern terminals do).
  • Basic shell knowledge: Familiarity with editing text files (e.g., using nano, vim, or gedit) and running terminal commands.

Understanding the Linux Shell Prompt#

The terminal prompt you see (e.g., user@hostname:~/project$ ) is controlled by a shell variable called PS1 (Prompt String 1). This variable defines the layout and content of the primary prompt.

To see your current PS1 value, run:

echo $PS1

Default PS1 values vary by shell and distribution but often include placeholders like:

  • \u: Current username
  • \h: Hostname (shortened)
  • \w: Current working directory (full path)
  • \$: Displays $ for regular users or # for root

Our goal is to modify PS1 to include the current Git branch (when in a Git repository) and add color.

Step 1: Identify Your Shell#

Most Linux systems use Bash (Bourne Again Shell) by default, but some use Zsh (Z Shell). To check your shell, run:

echo $SHELL
  • If the output is /bin/bash, follow the Bash instructions below.
  • If it’s /bin/zsh, skip to the Zsh adaptation section after the Bash steps.

Step 2: Create a Git Branch Detection Function#

To display the Git branch, we need a way to detect it. The git command provides this via:

git rev-parse --abbrev-ref HEAD

This returns the current branch name (e.g., main, feature/new-ui) if you’re in a Git repository. If not, it outputs nothing.

We’ll wrap this in a shell function to make it reusable. Open your shell configuration file (for Bash, this is ~/.bashrc; for Zsh, ~/.zshrc) using a text editor like nano:

nano ~/.bashrc

Scroll to the bottom of the file and add the following function:

# Function to get current Git branch (if in a Git repo)
git_branch() {
  # Use git rev-parse to get branch; suppress errors (e.g., if not a Git repo)
  local branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
  # If branch exists, return it in parentheses; otherwise, return empty
  if [ -n "$branch" ]; then
    echo " ($branch)"
  fi
}

Save the file (in nano, press Ctrl+O, then Enter, then Ctrl+X to exit).

Step 3: Modify the Prompt to Show the Git Branch#

Now, update PS1 to include the git_branch function’s output. In ~/.bashrc, find the existing PS1 line (it may look like PS1='\u@\h:\w\$ '). If there’s no PS1 line, add a new one.

Modify PS1 to include $(git_branch) where you want the branch to appear. For example, to show the branch after the working directory:

# Old PS1 (example)
# PS1='\u@\h:\w\$ '
 
# New PS1 with Git branch
PS1='\u@\h:\w$(git_branch)\$ '

Explanation:

  • \u@\h: Username and hostname (e.g., alice@my-laptop).
  • :\w: Current working directory (e.g., :~/projects/my-app).
  • $(git_branch): Inserts the branch name (e.g., (main)).
  • \$ : The $ prompt symbol.

Step 4: Add Color to the Git Branch (and Prompt)#

To make the branch stand out, add ANSI color codes to PS1. ANSI codes let you set text color, background color, and styles (bold, underline).

Common ANSI Color Codes#

CodeColor/Style
\033[0mReset (default)
\033[0;32mGreen (regular)
\033[1;32mGreen (bold)
\033[0;31mRed (regular)
\033[1;34mBlue (bold)
\033[1;36mCyan (bold)

Formatting the Prompt with Color#

Wrap the Git branch (and other prompt elements) in color codes. For example:

# Colored PS1: Username (blue), Hostname (cyan), Directory (green), Branch (purple)
PS1='\[\033[1;34m\]\u@\h\[\033[0m\]:\[\033[1;32m\]\w\[\033[0m\]\[\033[1;35m\]$(git_branch)\[\033[0m\]\$ '

Breakdown:

  • \[\033[1;34m\]: Start bold blue for \u@\h.
  • \[\033[0m\]: Reset to default (ends blue).
  • :\[\033[1;32m\]\w: Green bold for the working directory.
  • \[\033[1;35m\]$(git_branch): Purple bold for the Git branch.
  • \[\033[0m\]: Reset to default before the $ symbol.

Note: The \[ \] brackets tell Bash that the ANSI codes are non-printable characters, preventing line-wrapping glitches.

Step 5: Test Your New Prompt#

To apply the changes, reload your shell configuration:

source ~/.bashrc

Now, open a new terminal or navigate to a Git repository (e.g., cd ~/projects/your-git-repo). You should see:

alice@my-laptop:~/projects/your-git-repo (main)$  
  • The branch name (main) will appear in purple (or your chosen color).
  • If you navigate out of the Git repo, the branch disappears:
    alice@my-laptop:~/Downloads$  
    

Troubleshooting Common Issues#

1. Git Branch Not Showing Up#

  • Issue: The branch isn’t displayed even in a Git repo.
  • Fix:
    • Ensure Git is installed: git --version.
    • Verify the git_branch function is defined in ~/.bashrc (no typos!).
    • Run git rev-parse --abbrev-ref HEAD in the repo to test: it should return the branch name.

2. No Color in the Prompt#

  • Issue: The prompt has the branch but no color.
  • Fix:
    • Ensure your terminal supports ANSI colors (most modern terminals do; try GNOME Terminal or Konsole if unsure).
    • Check for typos in ANSI codes (e.g., \033 instead of \033).
    • Verify \[ \] brackets wrap non-printable codes (missing brackets cause color glitches).

3. Prompt Glitches (Line Wrapping)#

  • Issue: When typing long commands, the text overlaps itself.
  • Fix: Ensure all ANSI color codes are wrapped in \[ \] (e.g., \[\033[1;34m\] instead of \033[1;34m).

Adapting for Zsh (Optional)#

If you use Zsh, the process is similar but uses ~/.zshrc instead of ~/.bashrc. Here’s the adapted setup:

  1. Open ~/.zshrc:

    nano ~/.zshrc
  2. Add the git_branch function:

    git_branch() {
      local branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
      if [ -n "$branch" ]; then
        echo " ($branch)"
      fi
    }
  3. Modify PROMPT (Zsh uses PROMPT instead of PS1):

    PROMPT='%B%F{blue}%n@%m%f%b:%B%F{green}%~%f%b%B%F{purple}$(git_branch)%f%b $ '
    • %B: Bold on, %b: Bold off.
    • %F{blue}: Blue text, %f: Reset color.
    • %n@%m: Username@hostname, %~: Current directory (shorthand).
  4. Reload Zsh:

    source ~/.zshrc

Conclusion#

You’ve now configured your Linux terminal to display the current Git branch with color, just like Git Bash on Windows! This setup saves time, reduces errors, and makes your terminal more informative.

For advanced users, explore:

  • Adding a "dirty" indicator (e.g., * if there are uncommitted changes) using git diff --quiet in the git_branch function.
  • Customizing colors based on branch state (e.g., red for main, green for feature branches).

References#