-bash: __git_ps1: 找不到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15384025/
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
-bash: __git_ps1: command not found
提问by Joel Dehlin
I tried to install Ruby 2.0. My command line urped and now looks like the following:
我尝试安装 Ruby 2.0。我的命令行 urped,现在看起来如下所示:
-bash: __git_ps1: command not found
[11:58:28][whatever@whatever ~]$
I have not a clue how to get rid of the __git_ps1 command not found error. I've searched my .bash_profile and my .bashrc to see if it's trying to set a variable or something and am not seeing anything. The only place I can find git_ps1 mentioned is in ~/.dotfiles/.bash_prompt. I replace the content of that file completely, logout and log back in and it fixes nothing.
我不知道如何摆脱 __git_ps1 命令未找到错误。我搜索了我的 .bash_profile 和我的 .bashrc 以查看它是否正在尝试设置变量或其他内容,但没有看到任何内容。我能找到提到的 git_ps1 的唯一地方是在 ~/.dotfiles/.bash_prompt 中。我完全替换了该文件的内容,注销并重新登录,但没有任何修复。
I saw this, but I'm pretty new to command line so I just confused myself.
我看到了这个,但我对命令行很陌生,所以我只是让自己感到困惑。
Any ideas?
有任何想法吗?
采纳答案by David W.
BASH has a whole slew of ways of automatically setting your prompt to give you nice information. You set the prompt by setting the PS1
environment variable. For example, if I set PS1="$ "
my prompt will look like this:
BASH 有很多方法可以自动设置提示,为您提供很好的信息。您可以通过设置PS1
环境变量来设置提示。例如,如果我设置PS1="$ "
我的提示将如下所示:
$
Not too informative. All I can tell is that the command line is prompting me.
信息量不大。我只能说命令行正在提示我。
However, If I set PS1=\u@\h: \w$
, my prompt will now look like this:
但是,如果我设置了PS1=\u@\h: \w$
,我的提示现在看起来像这样:
david@vegibank:/usr/bin$
That tells me how I'm logged in (the \u
), the machine I'm onto (\h
), and the directory I'm in (the \w
). If I use git
, it would be nice if the git branch I'm in is also part of my prompt.
这告诉我我是如何登录的 (the \u
)、我所在的机器 ( \h
) 以及我所在的目录 (the \w
)。如果我使用git
,如果我所在的 git 分支也是我的提示的一部分,那就太好了。
This is exactly what is going on with your .profile
, your .bashrc
file, your .bash_login
or your .bash_profile
script. Or, what some system admin did in /etc/profile
.
这正是您的.profile
、您的.bashrc
文件、您的.bash_login
或您的.bash_profile
脚本正在发生的事情。或者,某些系统管理员在/etc/profile
.
There are a couple things you can do. Either:
您可以做几件事。任何一个:
- Download the missing
__git_ps1
and make sure it's in your$PATH
environment variable (which is set by a combination of the various initialization files mentioned above) - Change your
PS1
environment variable in whatever initialization file is being executed (I believe it is probably.bash_profile
.
- 下载丢失的
__git_ps1
并确保它在您的$PATH
环境变量中(由上述各种初始化文件的组合设置) PS1
在正在执行的任何初始化文件中更改您的环境变量(我相信它可能是.bash_profile
.
Just add this as the last line:
只需将其添加为最后一行:
PS1="\u@\h:\w\n$ "
The added \n
prints the dollar sign prompt on the line below like this:
添加\n
的在下面的行上打印美元符号提示,如下所示:
david@vegibank:/usr/bin
$
I like to do that because the prompt can get rather long and editing the command line gets tricky when the prompt is longer than 30 to 50 characters. Otherwise, it's pretty much the standard prompt that most users use. You can see more about setting BASH prompts in the man pages. (Search for the word Prompting on that page).
我喜欢这样做是因为提示会变得很长,并且当提示超过 30 到 50 个字符时,编辑命令行会变得棘手。否则,它几乎是大多数用户使用的标准提示。您可以在手册页中查看有关设置 BASH 提示的更多信息。(在该页面上搜索“提示”一词)。
If you find it a bit confusing, be glad you're not using Kornshell. I use Kornshell and to get the same prompt PS1=\u@\h:\w\n$
does, I set my prompt as:
如果您觉得它有点令人困惑,请庆幸您没有使用 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$ ")'
回答by Blake
Run the following:
运行以下命令:
$ curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
And add this to the top of your ~/.bashrc
:
并将其添加到您的顶部~/.bashrc
:
source ~/.bash_git
Re-login to your shell and you should be set.
重新登录到你的外壳,你应该被设置。
回答by Emil Lundberg
Search your system for a git-prompt.sh
, you need to source
that for the __git_ps1
function to be available. In Arch, it is currently located at /usr/share/git/completion/git-prompt.sh
. Add
在您的系统中搜索git-prompt.sh
,您需要source
使用该__git_ps1
功能才能使用该功能。在 Arch 中,它目前位于/usr/share/git/completion/git-prompt.sh
. 添加
source /path/to/git-prompt.sh
to some suitable shell script. If you're unsure where, add it to your ~/.bashrc
.
一些合适的shell脚本。如果您不确定在哪里,请将其添加到您的~/.bashrc
.
If you have locate
installed, you can use that to find the git-prompt.sh
file, but you may need to run updatedb
as root first.
如果您已经locate
安装,您可以使用它来查找git-prompt.sh
文件,但您可能需要先updatedb
以 root身份运行。
回答by ShubhamDhama
Quoting from /usr/lib/git-core/git-sh-prompt
:
引自/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
Following these steps should fix your issue!!
按照这些步骤应该可以解决您的问题!!
回答by sjas
As of 2019, the prompt helper function should be installed when installing the git
package and can be found at /usr/lib/git-core/git-sh-prompt
.
截至 2019 年,安装git
包时应安装提示助手功能,可在/usr/lib/git-core/git-sh-prompt
.
If it is not loaded, install bash-completion
package and have a look into your ~/.bashrc
.
如果未加载,请安装bash-completion
包并查看您的~/.bashrc
.
In my case I had to uncomment this:
就我而言,我不得不取消注释:
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
and open a new shell, and all was well.
并打开一个新的外壳,一切都很好。
Root cause was 'bash-completion` not being set up properly in the first place on a fresh install.
根本原因是在全新安装时没有正确设置“bash-completion”。
回答by apenas
I solved it simply using
我简单地解决了它
sudo apt install git
This appears because git is not installed and therefore the environment variable is not defined.
出现这种情况是因为未安装 git,因此未定义环境变量。