4

I use Vscode, and I want to know Where my HEAD is pointing branch,
How Can I show up the current branch name like Bash?
I use WSL(ubuntu)termimal in my Vscode and OS is Windows 10

Thank you

WSL image

rioV8
  • 24,506
  • 3
  • 32
  • 49
devstefancho
  • 2,072
  • 4
  • 17
  • 34

3 Answers3

5

Note that, from microsoft/vscode issue 67670, the current branch name is already visible in the status bar of VSCode.

branch VSCode

Or, with Git 2.22+ (Q2 2019)

git branch --show-current

It is true the prompt in a git bash in VSCode does not display the Git branch.

You need to configure the $SHELL

For example, to enable running bash as a login shell (which runs .bash_profile), pass in the -l argument (with double quotes):

// Linux
"terminal.integrated.shellArgs.linux": ["-l"]

Then in your ~/.bashrc can include a special prompt.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer, but I want to know exact way what I said in my question. because I think it's more intuitive and bettter. best regards – devstefancho Jun 26 '20 at 13:40
  • @stefancho I agree. That is why I added to the answer `git branch --show-current`, which is answering your original question – VonC Jun 26 '20 at 13:57
  • sorry for my poor question I think it was not well explained, what I want is that to know everytime, like bash. but git branch --show-current is only showing one time. – devstefancho Jun 26 '20 at 17:00
  • @stefancho THank you: this is clearer. I have updated the answer accordingly. – VonC Jun 26 '20 at 19:01
5

I got it configured by modifying the .bashrc file in the /home/ on the WSL session. You can do vim ~/.bashrc to edit the file.

Find and replace the code block in the .bashrc with this;

if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\$(`git branch --show-current 2>/dev/null`)\[\033[00m\]\$ '
else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$(`git branch --show-current 2>/dev/null`)\$ '
fi
Diganto Paul
  • 61
  • 1
  • 4
3

I found @Diganto Paul's answer does not show the current directory (as it was by default). I used this instead:

parse_git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[33m\]$(parse_git_branch)\[\033[00m\]\$ '

Copied from: https://hinty.io/ivictbor/show-git-branch-in-bash-on-linux-windows-wsl-2-cygwin-prompt/

Ojchris
  • 188
  • 1
  • 3
  • 11