-bash:__ git_ps1:找不到命令

问题描述 投票:50回答:5

我试图安装Ruby 2.0。我的命令行是urped,现在看起来如下所示:

-bash: __git_ps1: command not found
[11:58:28][whatever@whatever ~]$ 

我不知道如何摆脱__git_ps1命令找不到错误。我搜索了我的.bash_profile和我的.bashrc以查看它是否正在尝试设置变量或其他东西并且没有看到任何内容。我能找到git_ps1的唯一地方是〜/ .dotfiles / .bash_prompt。我完全替换该文件的内容,注销并重新登录,它什么也没有修复。

我看到this,但我对命令行很新,所以我只是困惑自己。

有任何想法吗?

bash shell command git-bash ruby-2.0
5个回答
22
投票

BASH有很多方法可以自动设置你的提示,为你提供很好的信息。您可以通过设置PS1环境变量来设置提示。例如,如果我设置PS1="$ ",我的提示将如下所示:

$ 

没有太多信息。我只能说命令行正在提示我。

但是,如果我设置PS1=\u@\h: \w$,我的提示现在将如下所示:

david@vegibank:/usr/bin$ 

这告诉我我是如何登录的(\u),我登上的机器(\h),以及我所在的目录(\w)。如果我使用git,如果我所在的git分支也是我的提示的一部分,那将是很好的。

这正是你的.profile,你的.bashrc文件,你的.bash_login或你的.bash_profile脚本正在发生的事情。或者,某些系统管理员在/etc/profile中做了什么。

你可以做几件事。或者:

  • 下载缺少的__git_ps1并确保它在你的$PATH环境变量中(由上面提到的各种初始化文件的组合设置)
  • 在正在执行的任何初始化文件中更改您的PS1环境变量(我相信它可能是.bash_profile

只需将其添加为最后一行:

PS1="\u@\h:\w\n$ "

添加的\n在下面的行上打印美元符号提示,如下所示:

david@vegibank:/usr/bin
$ 

我喜欢这样做,因为提示可能会变得相当长,当提示超过30到50个字符时,编辑命令行变得棘手。否则,它几乎是大多数用户使用的标准提示。您可以在man pages中查看有关设置BASH提示的更多信息。 (在该页面上搜索“提示”一词)。

如果你觉得它有点混乱,很高兴你没有使用Kornshell。我使用Kornshell并获得PS1=\u@\h:\w\n$提供的相同提示,我将提示设置为:

export PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'

112
投票

运行以下命令:

$ curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git

并将其添加到~/.bashrc的顶部:

source ~/.bash_git

重新登录你的shell,你应该设置。


40
投票

在你的系统中搜索git-prompt.sh,你需要source才能获得__git_ps1功能。在Arch,它目前位于/usr/share/git/completion/git-prompt.sh。加

source /path/to/git-prompt.sh

一些合适的shell脚本。如果您不确定在哪里,请将其添加到您的~/.bashrc

如果您安装了locate,可以使用它来查找git-prompt.sh文件,但您可能需要先以root身份运行updatedb


2
投票

引自/usr/lib/git-core/git-sh-prompt

# This script allows you to see repository status in your prompt.
#
# To enable:
#
#    1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-prompt.sh
#    3a) Change your PS1 to call __git_ps1 as
#        command-substitution:
#        Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
#        the optional argument will be used as format string.
#    3b) Alternatively, for a slightly faster prompt, __git_ps1 can
#        be used for PROMPT_COMMAND in Bash or for precmd() in Zsh
#        with two parameters, <pre> and <post>, which are strings
#        you would put in $PS1 before and after the status string
#        generated by the git-prompt machinery.  e.g.
#        Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
#          will show username, at-sign, host, colon, cwd, then
#          various status string, followed by dollar and SP, as
#          your prompt.
#        ZSH:  precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
#          will show username, pipe, then various status string,
#          followed by colon, cwd, dollar and SP, as your prompt.
#        Optionally, you can supply a third argument with a printf
#        format string to finetune the output of the branch status

遵循这些步骤应该解决您的问题!!


1
投票

自2019年起,安装git软件包时应安装提示辅助函数,可以在/usr/lib/git-core/git-sh-prompt找到。

如果没有加载,请安装bash-completion包并查看你的~/.bashrc

在我的情况下,我不得不取消注释:

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

并打开一个新壳,一切都很好。

根本原因是“bash-completion”在全新安装时没有正确设置。

© www.soinside.com 2019 - 2024. All rights reserved.