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:
|
|
2. Bash Completion (not stricly needed)
Very helpful to complete command by typing tab:
|
|
You should see something like /usr/share/bash-completion/bash_completion.
If missing you can install it with:
|
|
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
|
|
Step 2: Source it in .bashrc
At the end of your .bashrc, add:
|
|
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:
|
|
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:
|
|
Step 5: Reload Your Shell
Apply your changes by running:
|
|
Now navigate to a Git repo and verify the prompt shows correct indicators.
Prompt example:
|
|
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!