git (Mac) -bash: __git_ps1: 命令未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12870928/
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 14:45:54  来源:igfitidea点击:

(Mac) -bash: __git_ps1: command not found

macosgitterminalps1

提问by Geoff

I'm trying to change my command promt in terminal. I keep getting the error:

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

-bash: __git_ps1: command not found

-bash: __git_ps1: command not found

I've tried it just by typing it into the terminal as is: __git_ps1. I've also tried it out in the .bash_profile

我刚刚通过键入到终端作为是试了一下:__git_ps1。我也在.bash_profile

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

As you might be able to see/tell, yes, I do have the auto-completion installed and it does work great!

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

I came across this question: " PS1 env variable does not work on mac" which gives the code

我遇到了这个问题:“ PS1 env 变量在 mac 上不起作用”,它给出了代码

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

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

So I add it to my .bash_profilehoping that it will change something. Well, it did. It just changed the error output.

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

Here's the .bash_profilewith the addition:

这是.bash_profile添加内容:

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

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

And now here's the changed error output:

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

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

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

Note:I've also moved the alias below the source with no difference. I have git version 1.7.12.1

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

This should be a simple change. Can someone please help me?

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

Edit 10/13/12

编辑 10/13/12

No, I definitely do not want to define __git_ps1 myself but was just trying to see if it would be recognized by doing so. Yes, I have the .git-completion.bashfile installed. Here's how I got auto completion on my machine.

不,我绝对不想自己定义 __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

A ls -lathen lists the .git-completion.bashfile.

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

Edit 10/13/12 - Solved by Mark Longair(below)

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

The following code worked for me in the .bash_profilewhile others did not...

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

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

回答by Mark Longair

You've installed the version of git-completion.bashfrom master- in git's development history this is after a commit that split out the __git_ps1function from the completion functionality into a new file (git-prompt.sh). The commit that introduced this change, which explains the rationale, is af31a456.

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

I would still suggest that you just source the version of git-completion.bash(or git-prompt.sh) that is bundled with your installation of git.

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

However, if for some reason you still want to use this functionality by using scripts separately downloaded from master, you should download git-prompt.shsimilarly:

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

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

... and add the following line to your ~/.bash_profile:

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

source ~/.git-prompt.sh

Then your PS1variable that includes __git_ps1 '%s'should work fine.

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

回答by haysclark

After upgrading to OSX 10.9 Mavericks I had to reference the following files to get git shell command completion and git prompt to work again.

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

From my .bash_profile or similar:

从我的 .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)")$ '

回答by Steven Shaw

You should

你应该

$ brew install bash bash-completion git

$ brew install bash bash-completion git

Then source "$(brew --prefix)/etc/bash_completion" in your .bashrc.

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

回答by Usman

Following worked for me like a charm:

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

Run following in your Terminal:

在您的终端中运行以下命令:

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

Open/Create bash_profile:

打开/创建 bash_profile:

$ vi ~/.bash_profile

Add following to the file:

将以下内容添加到文件中:

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

Finally, source it using:

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

$ source ~/.bash_profile

This will solve the problem of bash: __git_ps1: command not found.

这将解决bash: __git_ps1: command not found.

Also your prompt will change to "os ". To change "os" to something else, modify "os" string in export PS1 line.

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

回答by Usman

Solution for MacOS Sierra and git version 2.10.1 <2017-2-06>

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

Step 1: Install the Git

第 1 步:安装 Git

You can skip this step if you already installed the latest git.

如果你已经安装了最新的 git,你可以跳过这一步。

Download git package from browser https://git-scm.com/download/

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

Note: if you install with curl [option] https://...option to download, you would have to make sure your system support SSL. So for new comer, to download from browser and install directly from git installer is much easier.

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

安装验证:
  • Show where is your git directory at: which git
  • Show which version your git currently is: git --versioncurrent version should be 2.10.1.
  • 显示您的 git 目录在哪里: which git
  • 显示您的 git 当前是哪个版本:git --version当前版本应该是 2.10.1。

Step 2: Add your git profile to your shell

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

  1. Open your shell profile:
    • nano ~/.bash_profileor nano ~/.bashrcDepends on where your modification is.
  2. Add the following code to the file:
    • source /usr/local/git/contrib/completion/git-completion.bash
    • source /usr/local/git/contrib/completion/git-prompt.sh
  1. 打开您的外壳配置文件:
    • nano ~/.bash_profilenano ~/.bashrc取决于您的修改位置。
  2. 将以下代码添加到文件中:
    • source /usr/local/git/contrib/completion/git-completion.bash
    • source /usr/local/git/contrib/completion/git-prompt.sh

Note: git installation location changed from opt/ directory to usr/local/ after OSX upgrade to El Capitain, and this is why some of the old answer above doesn't work anymore in MacOS Sierra.

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

  1. Add the following code to your PS1 configuration:

    • Option 1: add directly to your PS1: export PS1='\w$(__git_ps1 "(%s)") > '

      I prefer this simple approach since I already know the .git-completion.bashis there in my home directory, and I can add other prompt format in the front of it. here is my personal prompt for your reference: export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
    • Option 2: Add a selection script

    if [ -f ~/.git-completion.bash ]; then
          export PS1='\w$(__git_ps1 "(%s)") > '
    fi
  2. Save and use the profile: source ~/.bash_profileor source ~/.bashrc

  1. 将以下代码添加到您的 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 ]; then
          export PS1='\w$(__git_ps1 "(%s)") > '
    fi
  2. 保存并使用配置文件:source ~/.bash_profilesource ~/.bashrc

现在您应该看到 git 提示正常工作并显示您现在所在的分支。

回答by chovy

I had same problem when upgrading to Yosemite.

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

I just had to modify ~/.bashrcto source /usr/local/etc/bash_completion.d/git-prompt.shinstead of the old path.

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

then re-source your . ~/.bashrcto get the effect.

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

回答by luzik

High Sierra clean solution with colors !

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

No downloads. No brew. No Xcode

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

Just add it to your ~/.bashrc or ~/.bash_profile

只需将它添加到您的 ~/.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" "\$ "'

回答by traday

__git_ps1 for bash is now found in git-prompt.sh in /usr/local/etc/bash_completion.d on my brew installed git version 1.8.1.5

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

回答by slozier

this works in OS 10.8 in the .bash_profile

这适用于 .bash_profile 中的 OS 10.8

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

回答by Day Davis Waterbury

If you're hoping to use Homebrew to upgrade Git and you've let your system become out-of-date in general (as I did), you may need to bring Homebrew itself up-to-date first (as per brew update: The following untracked working tree files would be overwritten by merge:thanks @chris-frisina)

如果您希望使用 Homebrew 来升级 Git 并且您的系统总体上已经过时(就像我所做的那样),您可能需要先更新Homebrew 本身(根据brew update :以下未跟踪的工作树文件将被合并覆盖:谢谢@chris-frisina)

First bring Homebrew into line with the current version

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

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

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

Then update Git:

然后更新 Git:

brew upgrade git

酿造升级git

Problem Solved! ;-)

问题解决了!;-)