在提示中显示 git 分支名称在屏幕上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9717137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:12:36  来源:igfitidea点击:

Displaying git branch name in prompt does not work in screen

gitubuntu

提问by Ohad

I updated my .bashrc file as follows:

我更新了我的 .bashrc 文件如下:

PS1='[\u@\h \W$(__git_ps1 " (%s)")]$'

It works just find and I can see my branch name in the prompt. However,when I run "screen" , I get

它只是找到,我可以在提示中看到我的分支名称。但是,当我运行“屏幕”时,我得到

"-bash: __git_ps1: command not found"

“-bash:__git_ps1:找不到命令”

What can be the reason for this?

这可能是什么原因?

回答by SeriousM

This blog postexplains that you have to add the line source /etc/bash_completion.d/gitbefore you can use __git_ps1.

这篇博文解释说,您必须先添加该行,source /etc/bash_completion.d/git然后才能使用__git_ps1.

Here is the full example:

这是完整的示例:

source /etc/bash_completion.d/git  
export PS1='\w$(__git_ps1 "(%s)") > '

This also enables auto completion for branches.

这也为分支启用自动完成。

Using that formatting, your prompt will resemble (without colouring):

使用该格式,您的提示将类似于(不带颜色):

~/my-repo(master) > 

回答by msteiger

I find it cleaner to modify the existing prompt instead of defining a new one. The following snippet adds the git branch name to the existing prompt (which is $PS1). You can add the following snippet to the ~/.bashrcfile:

我发现修改现有提示而不是定义新提示更简洁。以下代码段将 git 分支名称添加到现有提示(即 $PS1)。您可以将以下代码段添加到~/.bashrc文件中:

source /etc/bash_completion.d/git (for Ubuntu 12.04 or less)
source /etc/bash_completion.d/git-prompt (for Ubuntu 13.04 and higher)
PS1=$PS1'$(__git_ps1 "(%s) ")'

If you want to have the branch name in color you can do that too: For example, the color green is defined as [\e[0;32m]. We add this to the inside string of the git_ps1 function and reset the color using \e[0m afterwards. The escaped brackets are required to indicate that "special" characters are inserted.

如果您希望分支名称为彩色,您也可以这样做:例如,绿色被定义为 [\e[0;32m]。我们将其添加到 git_ps1 函数的内部字符串中,然后使用 \e[0m 重置颜色。需要转义括号来指示插入了“特殊”字符。

PS1=$PS1'$(__git_ps1 "\[\e[0;32m\](%s) \[\e[0m\]")'

Many other color definitions can be found here

可以在此处找到许多其他颜色定义

回答by Chronial

The problem is that bash needs to be run as a login shell for this function to be available in the default cygwin setup. If you run bashin a cygwin bash you will have the same problem. To set screen to run bash in login mode, add this line to your ~/.screenrc file:

问题是 bash 需要作为登录 shell 运行,才能在默认的 cygwin 设置中使用此功能。如果您bash在 cygwin bash 中运行,您将遇到同样的问题。要将 screen 设置为在登录模式下运行 bash,请将此行添加到您的 ~/.screenrc 文件中:

shell -bash

回答by Mru

# Add following line to /.bashrc to show Git branch name in ssh prompt
PS1='\[3[0;31m\]\w\[3[0;33m\]$(__git_ps1)\[\e[0m\]$ '

\[\033[0;31m\]is red

\[\033[0;31m\]是红色的

\[\033[0;33m\]is yellow

\[\033[0;33m\]是黄色的

\[\e[0m\]is normal

\[\e[0m\]是正常的

回答by darksky

add source ~/.bash_profilein .bashrc.

source ~/.bash_profile.bashrc

Had the same problem and it just worked for me.

有同样的问题,它只对我有用。

回答by sjas

This was tested on debian/ubuntu.

这是在 debian/ubuntu 上测试的。



  1. install bash-completionpackage
  2. make sure the following lines exist in your ~/.bashrcand are not commented out.
  1. 安装bash-completion
  2. 确保以下行存在于您的文件中~/.bashrc并且没有被注释掉。


if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
     . /etc/bash_completion
fi

回答by Radon8472

If you dont have __git_ps1 you could use

如果你没有 __git_ps1 你可以使用

git branch --contains HEAD 2>/dev/null

It displays the same like __git_ps1.

它显示与 __git_ps1 相同。

And if you create an alias like this:

如果您创建这样的别名:

alias __git_ps1='git branch --contains HEAD 2>/dev/null'

e.g. your prombt you get with this command:

例如,您使用此命令得到的提示:

$PS1='[\u@\h \W(`__git_ps1`)]$'

or with

或与

PS1='[\u@\h \W\[3[36m\](`__git_ps1`)\[3[0m\]]$'

if you like colors

如果你喜欢颜色

Your scripts which use __git_ps1 and you promt will work perfect.

您使用 __git_ps1 和 promt 的脚本将完美运行。

回答by Ali Alp

root:~/project#  -> root:~/project(dev)# 

add the following code to the end of your ~/.bashrc

将以下代码添加到 ~/.bashrc 的末尾

force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/()/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[01;31m\]$(parse_git_branch)\[3[00m\]$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt