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
user@pc:~/development/my-repository$
# To this
user@pc:~/development/my-repository (main)$
Adding __git_ps1
The following code in .bashrc decides the command line display.
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
\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
(develop)
Open .bashrc and change the lines to the following.
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '
fi
then, reflect the change in the following command.
source ~/.bashrc
__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 <spearce@spearce.org>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# 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 git-prompt.sh for details.
#
Let’s copy the files to user’s home.
$ cp /usr/share/bash-completion/completions/git ~/.git-completion.bash
$ curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh --output ~/git-prompt.sh
After copying the files, load the files before setting PS1 in .bashrc
# Add the following two lines
source ~/.git-completion.bash
source ~/git-prompt.sh
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__get_ps1)\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__get_ps1)\$ '
fi
The current branch name will be shown after executing the following command.
source ~/.bashrc
Comments