Git How to show the current branch name on the terminal on WSL

eye-catch Other techs

When I used Git on WSL, the current branch name is not shown. I always needed to execute the command git status to know the current branch. I don’t want to do it every time.

# From this

# To this
user@pc:~/development/my-repository (main)$
Sponsored links

Adding __git_ps1

The following code in .bashrc decides the command line display.

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

\u@h:\w\$ means…

\u: user name
\h: host name
\w: current working directory
$: show $ for normal user, show # for root user

We can use __get_ps1 command to show the current branch name.

$  __git_ps1

Open .bashrc and change the lines to the following.

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

then, reflect the change in the following command.

source ~/.bashrc
Sponsored links

__git_ps1: command not found

If you get command not found error, you need to load two files in .bashrc.

When I looked at some articles, they mentioned that bash_completion.d is located in /usr/local/etc/. However, it is empty on my machine. Instead, I found git file in /usr/share/bash-completion/completions/.

$ head -n 30 /usr/share/bash-completion/completions/git
# bash/zsh completion support for core Git.
# Copyright (C) 2006,2007 Shawn O. Pearce <>
# Conceptually based on gitcompletion (
# Distributed under the GNU General Public License, version 2.0.
# The contained completion routines provide support for completing:
#    *) local and remote branch names
#    *) local and remote tag names
#    *) .git/remotes file names
#    *) git 'subcommands'
#    *) git email aliases for git-send-email
#    *) tree paths within 'ref:path/to/file' expressions
#    *) file paths within current working directory and index
#    *) common --long-options
# To use these routines:
#    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-completion.bash
#    3) Consider changing your PS1 to also show the current branch,
#       see for details.

Let’s copy the files to user’s home.

$ cp /usr/share/bash-completion/completions/git ~/.git-completion.bash
$ curl --output ~/

After copying the files, load the files before setting PS1 in .bashrc

# Add the following two lines
source ~/.git-completion.bash
source ~/

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

The current branch name will be shown after executing the following command.

source ~/.bashrc


Copied title and URL