如何在 WSL 中显示当前分支(我使用 Vscode)

ste*_*cho 4 git bash visual-studio-code windows-subsystem-for-linux

我使用 Vscode,我想知道我的 HEAD 指向哪里分支,
如何像 Bash 一样显示当前分支名称?
我在 Vscode 中使用 WSL(ubuntu)termimal,操作系统是 Windows 10

谢谢

WSL图像

Von*_*onC 6

请注意,从microsoft/vscode问题 67670开始,当前分支名称已经在 VSCode 的状态栏中可见。

分支 VSCode

或者,使用 Git 2.22+(2019 年第 2 季度)

git branch --show-current
Run Code Online (Sandbox Code Playgroud)

确实,VSCode 中 git bash 的提示不显示 Git 分支

您需要配置$SHELL

例如,要启用 bash 作为登录 shell 运行(运行.bash_profile),请传入-l参数(带双引号):

// Linux
"terminal.integrated.shellArgs.linux": ["-l"]
Run Code Online (Sandbox Code Playgroud)

然后您~/.bashrc可以在其中包含一个特殊提示


小智 6

我通过修改 WSL 会话上 /home/ 中的 .bashrc 文件来配置它。您可以执行 vim ~/.bashrc 来编辑该文件。

查找并替换 .bashrc 中的代码块;

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
Run Code Online (Sandbox Code Playgroud)