Featured image of post How to Integrate Git Status Indicators into the Bash Prompt

How to Integrate Git Status Indicators into the Bash Prompt

Learn how to enhance your Git workflow by configuring a smart Bash prompt that displays branch names, commit status, and changes at a glance.

Introduction

Working with Git in the terminal can sometimes be tricky and time-consuming, especially when you’re juggling multiple branches or trying to remember if you’ve committed your latest changes. In this article, I’ll walk you through a step-by-step guide to customizing your Bash prompt in WSL2 to display Git branch names, status indicators, and upstream tracking info. So you can work faster, stay informed, and stop typing git status every five seconds.


Prerequisites

Before proceeding, make sure the following tools are installed and available:

1. Git

Of course, let’s start by installing git:

1
sudo apt install git

2. Bash Completion (not stricly needed)

Very helpful to complete command by typing tab:

1
locate bash_completion

You should see something like /usr/share/bash-completion/bash_completion.

If missing you can install it with:

1
sudo apt install bash-completion

Prompt using custom function

You can create your own Git prompt parser for more control. Inspired by Matthew McCullough’s repo.

Here’s how I configured it.

Step 1. Create the function

First, create the file ~/.bash_gitprompt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
cat ~/.bash_gitprompt

parse_git_branch() {
  local git_status
  git rev-parse --git-dir &>/dev/null || return
  git_status="$(git status 2>/dev/null)"

  local branch remote state

  if [[ ! "${git_status}" =~ "working directory clean" ]]; then
    state="⚡"
  fi

  if [[ "${git_status}" =~ "Your branch is ahead" ]]; then
    remote="↑"
  elif [[ "${git_status}" =~ "Your branch is behind" ]]; then
    remote="↓"
  elif [[ "${git_status}" =~ "have diverged" ]]; then
    remote="↕"
  fi

  if [[ "${git_status}" =~ "On branch " ]]; then
    branch=$(git rev-parse --abbrev-ref HEAD)
    printf " (%s)%s%s" "$branch" "${remote:-}" "${state:-}"
  fi
}

prompt_func() {
  local ret=$?
  local user_color="\[\e[1;32m\]"   # bright green
  local host_color="\[\e[1;36m\]"   # bright cyan
  local path_color="\[\e[1;34m\]"   # bright blue
  local reset_color="\[\e[0m\]"
  local gitinfo="\[\e[0;32m\]$(parse_git_branch)\[$reset_color\]"
  local prompt_symbol

  if [ "$EUID" -eq 0 ]; then
    prompt_symbol="#"
  else
    prompt_symbol="$"
  fi

  # Terminal/tab title escape sequence (properly wrapped in \[ \])
  local title="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]"

  PS1="${title}${user_color}\u${host_color}@\h${path_color}:\w${gitinfo} ${reset_color}${prompt_symbol} "

  if [ $ret -ne 0 ]; then
    PS1="${PS1}\[\e[0;31m\]✘\[\e[0m\] "
  fi
}

export PROMPT_COMMAND=prompt_func

Step 2: Source it in .bashrc

At the end of your .bashrc, add:

1
source ~/.bash_gitprompt

Step 3: Remove XTerm Title Setting

This block sets the XTerm tab title that displays user@host: path in the terminal tab (e.g., in WSL2 or Windows Terminal), not in the prompt.

Because we have defined it into the .bash_gitprompt file, you can delete the section related to the xterm PS1 setting defined by default in .bashrc.

Open .bashrc and remove or comment that part:

1
2
3
4
5
6
7
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

Step 4: Remove default PS1 logic (optional)

The PROMPT_COMMAND overrides the PS1 variable, so you can remove or comment the default logic inside the .bashrc that looks like this:

1
2
3
4
5
6
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

Step 5: Reload Your Shell

Apply your changes by running:

1
source ~/.bashrc

Now navigate to a Git repo and verify the prompt shows correct indicators.

Prompt example:

1
jamy@host:~/repo (main↑↓⚡) $

This is how Git states will appear in your prompt:

  • branch ahead
  • branch behind
  • branch diverged
  • working directory has uncommitted changes
  • last command failed (non-zero exit code)

Conclusion

I highly recommend setting up a Git-aware prompt in bash. It’s simple to configure and incredibly useful.

Since enabling this Git prompt, I can instantly tell if I’m ahead, behind, or forgot to push my changes. It’s a small tweak that makes a significant difference.

You can find the code from this article by clicking the GitHub logo below.

Thanks for reading this article, I hope this makes your terminal experience easier and your Git workflow smoother!

Github