【问题标题】:Add git branch to bash prompt [duplicate]将 git 分支添加到 bash 提示符 [重复]
【发布时间】:2020-12-16 18:43:32
【问题描述】:

我正在尝试将我当前的 git 分支添加到我的 bash 终端提示符中。我已将此文件:https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh 复制到我的主目录,这是 .bashrc 中与我的提示相关的部分:

source ~/.git-prompt.sh

if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;31m\] \W$(__git_ps1 " (%s)")\[\033[00m\]\$ '
else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\ \W$(__git_ps1 " (%s)")$ '
fi

但是,\W$(__git_ps1 " (%s)") 正在返回当前文件夹和分支,并在不在存储库中时返回当前目录。

是否可以让它只在存储库中显示当前分支,而当我不在一个存储库中时,什么都不显示?

【问题讨论】:

  • 鼓励您朝着正确的方向前进:您的问题已被很好地张贴、记录等,但不幸的是,它是重复的。

标签: bash git


【解决方案1】:

我发现 \W 正在插入当前目录,该目录在源代码中或人们谈论实现的任何地方都没有注释。我已经解决了这个问题:

PS1='${debian_chroot:+($debian_chroot)}\[\033[32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;31m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '

【讨论】:

猜你喜欢
  • 2013-03-30
  • 2010-10-23
  • 2014-05-24
  • 1970-01-01
  • 2012-08-29
  • 2013-05-08
  • 2021-09-10
  • 2014-02-10
  • 1970-01-01
相关资源
最近更新 更多