ChatGPT解决这个技术问题 Extra ChatGPT

(Mac) -bash: __git_ps1: 找不到命令

我正在尝试在终端中更改我的命令提示符。我不断收到错误:

-bash: __git_ps1: command not found

我只是通过在终端中输入它来尝试它:__git_ps1。我也在 .bash_profile 中尝试过

if [ -f ~/.git-completion.bash ]; then
  source ~/.git-completion.bash
  export PS1='[\W]$(__git_ps1 "(%s)"): '
fi

正如您可能看到/告诉的那样,是的,我确实安装了自动完成功能,而且效果很好!

我遇到了这个问题:“PS1 env variable does not work on mac”给出了代码

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"

所以我将它添加到我的 .bash_profile 中,希望它会有所改变。嗯,确实如此。它只是改变了错误输出。

这是添加的 .bash_profile

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"

if [ -f ~/.git-completion.bash ]; then
  source ~/.git-completion.bash
  export PS1='[\W]$(__git_ps1 "(%s)"): '
fi

现在这是更改后的错误输出:

sed: (%s): No such file or directory

注意:我还将别名移到源下方,没有任何区别。我有 git 版本 1.7.12.1

这应该是一个简单的改变。有人可以帮帮我吗?

2012 年 10 月 13 日编辑

不,我绝对不想自己定义 __git_ps1 ,只是想看看这样做是否会被识别。是的,我已经安装了 .git-completion.bash 文件。这是我如何在我的机器上自动完成。

cd ~
curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
mv ~/git.completion.bash ~/.git-completion.bash

ls -la 然后列出 .git-completion.bash 文件。

编辑 10/13/12 - Mark Longair 解决(下)

以下代码在 .bash_profile 中对我有用,而其他代码则没有...

if [ -f ~/.git-prompt.sh ]; then
  source ~/.git-prompt.sh
  export PS1='Geoff[\W]$(__git_ps1 "(%s)"): '
fi
我的 git-completion.bash__git_ps1 定义为 shell 函数。你的呢?你怎么知道完成是有效的?当您键入 git h 时会发生什么?
当我输入它时,它以 git help[space] 结尾
接受的答案很好,但也可以通过点击 git <tab> 来启用提示,如 Mark Longair 引用的提交中所述。
这不仅是一个很好的解释性问题,而且是指导解决方案的有用资源。当之无愧的赞成票。
在 OS X 10.8.5 上的 screen(1) 内运行 bash 时,-f 检查可修复此错误。不知道为什么,因为文件无论如何都存在,但还是要感谢!

M
MBlanc

您已经从 master 安装了 git-completion.bash 的版本 - 在 git 的开发历史中,这是在将 __git_ps1 函数从完成功能拆分为新文件 (git-prompt.sh) 的提交之后。引入此更改并解释其基本原理的提交是 af31a456

我仍然建议您只获取与您的 git 安装捆绑在一起的 git-completion.bash(或 git-prompt.sh)版本。

但是,如果出于某种原因您仍想通过使用从 master 单独下载的脚本来使用此功能,您应该类似地下载 git-prompt.sh

curl -o ~/.git-prompt.sh \
    https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

...并将以下行添加到您的 ~/.bash_profile

source ~/.git-prompt.sh

那么包含 __git_ps1 '%s'PS1 变量应该可以正常工作。


你说得对,我不想自己定义 __git_ps1 。我只是想看看它是否会识别它而它不会。我的根目录上有 .git-completion.bash 。我将更新我上面的问题,包括我如何让自动完成工作......
@designer84:感谢您提供的额外信息-我认为现在问题很清楚了,并且我已经通过解释更新了我的答案。
为什么这不起作用?如果 [ -f /usr/local/git/contrib/completion/git-completion.bash ];然后 source /usr/local/git/contrib/completion/git-completion.bash export PS1='[\W]$(__git_ps1 "(%s)"): ' fi
在通过 Homebrew 安装了 git 1.8.5.3 的 OSX 10.9 上,git-prompt.shgit-completion.bash 都在 `brew --prefix git`/etc/bash_completion.d/ 中。
在 MacPorts 中,当包 git-coregit 替换时,路径发生了变化,请参阅 this question
h
haysclark

升级到 OSX 10.9 Mavericks 后,我必须参考以下文件才能完成 git shell 命令并让 git 提示符再次工作。

从我的 .bash_profile 或类似的:

if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash ]; then
    . /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
fi

source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh

#shell prompt example
PS1='\u $(__git_ps1 "(%s)")\$ '

这也是我在 osv 10.9 中的问题
虽然@marklongair 接受的回复是一个很好的描述,但我通过谷歌搜索错误消息(升级到 OS 10.10 后)发现了这个问题,这个答案是我的问题的最简单解决方案(只需更改新位置的路径)。
这在 10.10.5 Yosemite 中对我有用,但是我将 git-prompt 的位置更改为 ~/.git_prompt.sh。其他解决方案不起作用。
我将您建议的行添加到 .bash_profile,但仍然收到错误:bash: __docker_machine_ps1: command not found
S
Steven Shaw

你应该

brew install bash bash-completion git

然后在 .bashrc 中获取“$(brew --prefix)/etc/bash_completion”。


很好的答案,如果 git 与 brew 一起安装,绝对是最好的方法。但我认为你需要来源:source $(brew --prefix)/etc/bash_completion.d/git-prompt.sh
@febLey 是的,位置变了。我已经更新了答案。谢谢。
这行不通。我收到错误:bash:/usr/local/etc/bash_completion.d/git-prompt.sh:权限被拒绝 bash:__docker_machine_ps1:找不到命令
U
Usman

以下对我来说就像一个魅力:

在终端中运行以下命令:

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

打开/创建 bash_profile:

$ vi ~/.bash_profile

在文件中添加以下内容:

source ~/.bash_git
export PS1='\[\033[01;32m\]os \[\033[01;34m\]\w $(__git_ps1 "[%s]")\$\[\033[00m\] '
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"

最后,使用以下方法获取它:

$ source ~/.bash_profile

这将解决 bash: __git_ps1: command not found 的问题。

您的提示也将更改为“os”。要将“os”更改为其他内容,请修改导出 PS1 行中的“os”字符串。


每次进入 Docker CLI 时,我仍然收到错误 sh -c "clear && /bin/bash" bash: __docker_machine_ps1: command not found 。
佚名

MacOS Sierra 和 git 版本 2.10.1 的解决方案 <2017-2-06>

第 1 步:安装 Git

如果您已经安装了最新的 git,则可以跳过此步骤。

从浏览器下载 git 包 https://git-scm.com/download/

注意:如果您安装时使用 curl [option] https://... 选项下载,您必须确保您的系统支持 SSL。所以对于新手来说,从浏览器下载并直接从 git 安装程序安装要容易得多。

显示你的 git 目录在哪里:which git

显示你的 git 当前是哪个版本: git --version 当前版本应该是 2.10.1。

第 2 步:将您的 git 配置文件添加到您的 shell

打开你的 shell 配置文件: nano ~/.bash_profile 或 nano ~/.bashrc 取决于你的修改在哪里。将以下代码添加到文件中: source /usr/local/git/contrib/completion/git-completion.bash source /usr/local/git/contrib/completion/git-prompt.sh

注意:在 OSX 升级到 El Capitain 后,git 安装位置从 opt/ 目录更改为 usr/local/,这就是为什么上面的一些旧答案在 MacOS Sierra 中不再适用的原因。

将以下代码添加到您的 PS1 配置中: 选项 1:直接添加到您的 PS1:export PS1='\w$(__git_ps1 "(%s)") > ' 我更喜欢这种简单的方法,因为我已经知道 .git-completion .bash 在我的主目录中,我可以在它的前面添加其他提示格式。这是我个人的提示供您参考:export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> ' 选项2:添加选择脚本 if [ -f ~/.git-completion.bash ];然后 export PS1='\w$(__git_ps1 "(%s)") > ' fi 保存并使用配置文件: source ~/.bash_profile 或 source ~/.bashrc


l
luzik

带有颜色的 High Sierra 清洁解决方案!

没有下载。没有酿造。没有 Xcode

只需将其添加到您的 ~/.bashrc 或 ~/.bash_profile

export CLICOLOR=1
[ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh ] && . /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
PROMPT_COMMAND='__git_ps1 "\h:\W \u" "\\\$ "'

c
chovy

升级到优胜美地时我遇到了同样的问题。

我只需要将 ~/.bashrc 修改为 source /usr/local/etc/bash_completion.d/git-prompt.sh 而不是旧路径。

然后重新获取您的 . ~/.bashrc 以获得效果。


t
traday

bash 的 __git_ps1 现在在我的 brew 安装的 git 版本 1.8.1.5 的 /usr/local/etc/bash_completion.d 的 git-prompt.sh 中找到


s
slozier

这适用于 .bash_profile 中的 OS 10.8

if [ -f ~/.git-prompt.sh ]; then
  source ~/.git-prompt.sh
  export PS1='YOURNAME[\W]$(__git_ps1 "(%s)"): '
fi

不修复错误:bash: __docker_machine_ps1: command not found
v
velop

对于 macports,我必须将:source /opt/local/share/git-core/git-prompt.sh 添加到我的 ./profile


@IgorGanapolsky 我猜你没有安装 macports ?只需执行 touch ~/.profile。但你显然需要一个文件:/opt/local/share/git-core/git-prompt.sh
C
Community

如果您希望使用 Homebrew 升级 Git,并且您的系统总体上已经过时(就像我所做的那样),您可能需要先将 Homebrew 本身更新(根据 {1 } 感谢@chris-frisina)

首先使 Homebrew 与当前版本保持一致

cd /usr/local git fetch origin git reset --hard origin/master

然后更新 Git:

酿造升级 git

问题解决了! ;-)


j
jrc

至少在 Xcode 6 中,您已经拥有 git-completion.bash。它在 Xcode 应用程序包中。

只需将其添加到您的 .bashrc 中:

source `xcode-select -p`/usr/share/git-core/git-completion.bash

J
JCLopez

从此 Git 完成下载文件 git-prompt.sh 和 git-completion.bash 重命名文件。将这些文件移动到您的主目录。将源文件添加到 .bash_profile 源 ~/git-completion0.bash 源 ~/git-prompt0.sh 和四个以触发代码块。


o
op1ekun

我知道这不是一个真正的答案...

我在 .bashrc 中采购 git-prompt.sh 时遇到了一些奇怪的问题,所以我开始寻找其他解决方案。这个:http://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ 不使用 __git_ps1 并且作者声称它也可以在 Mac 上运行(现在它在我的 Ubuntu 上完美运行并且很容易调整)。

我希望它有帮助!


s
superheron

我正在为 git hub 上 Udacity 的课程,并且遇到了同样的问题。这是我的最终代码,它可以正常工作。

# Change command prompt
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \ .   (.*\)/(\1)/'"

if [ -f ~/.git-completion.bash ]; then
  source ~/.git-completion.bash
  export PS1='[\W]$(__git_ps1 "(%s)"): '
fi

source ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"

有用! https://i.stack.imgur.com/d0lvb.jpg


h
hardeep
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.git-prompt.bash

[[ -f ~/.git-prompt.bash ]]       && . ~/.git-prompt.bash
# Available GIT_PS1 options/env vars

cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq | sed -r 's,^(.*)$,export \1=,'
export GIT_PS1_COMPRESSSPARSESTATE=
export GIT_PS1_DESCRIBE_STYLE=
export GIT_PS1_HIDE_IF_PWD_IGNORED=
export GIT_PS1_OMITSPARSESTATE=
export GIT_PS1_SHOWCOLORHINTS=
export GIT_PS1_SHOWDIRTYSTATE=
export GIT_PS1_SHOWSTASHSTATE=
export GIT_PS1_SHOWUNTRACKEDFILES=
export GIT_PS1_SHOWUPSTREAM=
export GIT_PS1_STATESEPARATOR=

for i in $(cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq); do varname=$i; declare -g ${i}=1; done
# P.S Above is just illustration not all config vars are [0/1].

# For more info: 

cat ~/.git-prompt.bash | sed -r -n -e '1,/^[^\s*#]/p' | head -n -2

一个不错的改进可能是将您刚刚处于灰色状态的上一个分支添加到提示符中。
J
Joshua Muheim

这个对我有用,它有彩色的 git 输出和提示中的指示文件是否已更改/已添加,直接放入其中:

GIT_PS1_SHOWDIRTYSTATE=true

. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-completion.bash
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-prompt.sh

PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

一定要使用正确的路径!我使用 homebrew 安装 git,使用 brew list git 获取当前安装的路径。

最好不要使用硬编码路径,但不知道如何获取当前安装的路径。

更多信息在这里:http://en.newinstance.it/2010/05/23/git-autocompletion-and-enhanced-bash-prompt/


l
liudongmiao

对于 git,有 /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh。也请看/etc/bashrc_Apple_Terminal

所以,我把这些放在 ~/.bash_profile 中:

if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh ]; then
  . /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
  export GIT_PS1_SHOWCOLORHINTS=1
  export GIT_PS1_SHOWDIRTYSTATE=1
  PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }__git_ps1 '\u:\w' '\\\$ '"
fi

仍然收到此错误:bash: __docker_machine_ps1: command not found
M
MikeiLL

我刚刚在 Mojave 上安装的另一个选项:magicmonty/bash-git-prompt

运行 (brew update) 和 brew install bash-git-promptbrew install --HEAD bash-git-prompt

然后到您的 ~/.bash_profile~/.bashrc

if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
  __GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
  GIT_PROMPT_ONLY_IN_REPO=1
  source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi

我很高兴。


R
Ruben Helsloot

请注意,如果您没有通过 Xcode 或 home-brew 安装 git,您可能会在 /Library/Developer/CommandLineTools/ 中找到 haysclarks 引用的 bash 脚本,而不是在 /Applications/Xcode.app/Contents/Developer/ 中,从而使这些行包含在 .bashrc 中以下:

if [ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ]; then
    . /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
fi

source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh

如果您也希望使用 git-prompt,您将需要这些行。 [1]:https://stackoverflow.com/a/20211241/4795986


M
Mohammed Alhussaini

复制/下载以下文件并将它们复制到主目录:~/

git-completion.bash

git-prompt.sh

对于 bash_profile,在开头添加:

source ~/git-completion.bash
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1

For more and easy downloads you could check this