-bash:__ git_ps1:找不到命令

时间:2013-03-13 11:28:28

标签: bash shell command git-bash ruby-2.0

我尝试安装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,但我对命令行很新,所以我只是把自己搞糊涂了。

有什么想法吗?

6 个答案:

答案 0 :(得分:110)

运行以下命令:

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

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

source ~/.bash_git

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

答案 1 :(得分:40)

在您的系统中搜索git-prompt.sh,您需要source __git_ps1功能才能使用/usr/share/git/completion/git-prompt.sh。在Arch中,它目前位于source /path/to/git-prompt.sh 。添加

~/.bashrc

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

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

答案 2 :(得分:23)

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$ ")'

答案 3 :(得分: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

按照以下步骤操作可以解决您的问题!

答案 4 :(得分:0)

从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”。

答案 5 :(得分:0)

我只需使用即可解决

sudo apt install git

出现此现象是因为未安装git,因此未定义环境变量。

相关问题