在 Windows Git Bash 中安装 bash-git-prompt

2,137 阅读2分钟

需求

我经常会在项目根目录中右键点开 Git Bash 进行一些命令行操作,默认窗口中会显示当前文件目录和 git 分支名,但我希望能看到更多信息,如是否存在未提交的修改、未推送的提交等,这样我在每天打开 bash 窗口后就能对当前项目的状态有个大概的了解。

搜索

我在网上查阅了一番,最终决定使用 bash-git-prompt

结果

先放上最终的效果对比图:

默认窗口

默认窗口
安装 bash-git-prompt
安装 bash-git-prompt 后

相关含义如下(详情查看 bash-git-prompt 官方说明):

(master↑3|✚1): on branch master, ahead of remote by 3 commits, 1 file changed but not staged
(status|●2): on branch status, 2 files staged
(master|✚7…): on branch master, 7 files changed, some files untracked
(master|✖2✚3): on branch master, 2 conflicts, 3 files changed
(master|⚑2): on branch master, 2 stash entries
(experimental↓2↑3|✔): on branch experimental; your branch has diverged by 3 commits, remote by 2 commits; the repository is otherwise clean
(:70c2952|✔): not on any branch; parent commit has hash 70c2952; the repository is otherwise clean

实现

找到你安装 Git 的地址,在 Git/etc 目录下将 bash-git-prompt 项目 clone 下来:

git clone https://github.com/magicmonty/bash-git-prompt.git

然后用编辑器打开 Git/etc 目录下的 bash.bashrc 文件,注释其中全部代码,新增如下三行代码:

GIT_PROMPT_ONLY_IN_REPO=1
GIT_PROMPT_THEME=Evermeet  # 主题可以自己选,在 Git\etc\.bash-git-prompt\themes 中挑一个
source /etc/.bash-git-prompt/gitprompt.sh

保存文件后,重新打开 Git Bash 窗口,Voila!安装好了~

问题

如果你发现窗口中的打勾和箭头等符号显示有问题,可以这样做: 右键窗口标题栏 -> Options -> Text -> Font Select -> DejaVu Sans Mono

参考Strange character

The font limitation shows up in the default Ubuntu shell on Windows 10 as well but I was able to see the glyph characters by simply using DejaVu Mono dejavu-fonts.github.io/. What @scotlynhatt means is download the font on windows, install it and then select it from the font menu for the WSL window (right click on the title bar -> properties -> font).