bash - 提示中的 Git 分支

标签 bash rc

有人能解释一下为什么分支名称没有显示在我的 (bash) 提示符上吗?
我使用的是ubuntu 16.10。我尝试使用此 site 中的说明.

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

我的.bashrc文件在这里:https://github.com/JeremieGauthier/.bashrc/blob/master/.bashrc

我也尝试了以下代码,但它也不起作用。

function color_my_prompt {
    local __user_and_host="\[\033[01;32m\]\u@\h"
    local __cur_location="\[\033[01;34m\]\w"
    local __git_branch_color="\[\033[31m\]"
    #local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
    local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
    local __prompt_tail="\[\033[35m\]$"
    local __last_color="\[\033[00m\]"
    export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt

最佳答案

安装时 git 附带 git-prompt.sh bash 函数 __git_ps1 来更新提示。

功能很简单,当你在 git repo 目录中 cd 时添加当前分支。

只需添加对此函数或 ps1 中可用的另一个函数的调用,并确保您的 .bashrc.bash_profile 加载bash_completion 目录。

如果您的 git 版本不包含 git_prompt.sh 脚本,请手动下载它并按照它提供的说明进行操作。

关于bash - 提示中的 Git 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45968420/

相关文章:

ruby - .gemrc 评论?

c - vi/vim - 根据代码中特殊文件或标签的存在自定义格式

windows - 如何在 Windows CMD 中启用 bash 命令?

matlab - matlab 有 matlabrc 文件吗?

angularjs - Angular2 RC1 多级 routerLink 指令

CMake 找不到 windres.exe (MingW),CMAKE_RC_COMPILER

bash - 在 bash 脚本中使用 Unix 域套接字的最佳方式

linux - 使用 shell 脚本 (bash) 查找我的系统的特定接口(interface)的 IP 地址

linux - 在远程 ubuntu 机器上执行 bash 命令时出错 "no tty present and no askpass program specified"

linux - 编码问题?