【问题标题】:(Mac) -bash: __git_ps1: command not found(Mac) -bash: __git_ps1: 找不到命令
【发布时间】:2012-10-03 23:15:41
【问题描述】:

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

-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

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

编辑 10/13/12

不,我绝对不想自己定义 __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 上的屏幕 (1) 内运行 bash 时,-f 检查可修复此错误。不知道为什么,因为文件无论如何都存在,但还是要感谢!

标签: macos git terminal ps1


【解决方案1】:

您已经从 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
  • 在 OSX 10.9 上,通过 Homebrew 安装了 git 1.8.5.3,git-prompt.shgit-completion.bash 都可以在 `brew --prefix git`/etc/bash_completion.d/ 中找到。
  • 在MacPorts中,当包git-coregit替换时路径发生了变化,见this question
【解决方案2】:

升级到 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
【解决方案3】:

你应该

$ 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: Permission denied bash: __docker_machine_ps1: command not found
【解决方案4】:

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

在终端中运行以下命令:

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
【解决方案5】:

MacOS Sierra 和 git 版本 2.10.1 的解决方案

第 1 步:安装 Git

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

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

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

安装验证:
  • 显示你的 git 目录在哪里:which git
  • 显示你的 git 当前是哪个版本:git --version 当前版本应该是 2.10.1。

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

  1. 打开您的外壳配置文件:
    • nano ~/.bash_profilenano ~/.bashrc 取决于您的修改位置。
  2. 将以下代码添加到文件中:
    • 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 中不再适用的原因。

  1. 将以下代码添加到您的 PS1 配置中:

    • 选项 1:直接添加到您的 PS1:export PS1='\w$(__git_ps1 "(%s)") > '

      我更喜欢这种简单的方法,因为我已经知道 .git-completion.bash 在我的主目录中,我可以在它的前面添加其他提示格式。这是我的个人提示供您参考:export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
    • 选项 2:添加选择脚本

    如果 [ -f ~/.git-completion.bash ];然后
          导出 PS1='\w$(__git_ps1 "(%s)") > '
    菲
  2. 保存并使用配置文件:source ~/.bash_profilesource ~/.bashrc

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

【讨论】:

    【解决方案6】:

    带有颜色的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" "\\\$ "'
    

    【讨论】:

      【解决方案7】:

      升级到 Yosemite 时我遇到了同样的问题。

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

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

      【讨论】:

        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

          这适用于 .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
          【解决方案10】:

          对于 macport,我必须在我的 ./profile 中添加:source /opt/local/share/git-core/git-prompt.sh

          【讨论】:

          • @IgorGanapolsky 我猜你没有安装 macports?只需执行touch ~/.profile。但你显然需要一个文件:/opt/local/share/git-core/git-prompt.sh
          【解决方案11】:

          如果您希望使用 Homebrew 来升级 Git,并且您的系统总体上已经过时(就像我所做的那样),您可能需要先将 Homebrew 本身更新(如每brew update: The following untracked working tree files would be overwritten by merge:感谢@chris-frisina)

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

          cd /usr/local
          git 获取来源
          git reset --hard origin/master

          然后更新 Git:

          brew 升级 git

          问题解决了! ;-)

          【讨论】:

            【解决方案12】:

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

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

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

            【讨论】:

              【解决方案13】:
              1. Git completion 下载文件 git-prompt.shgit-completion.bash
              2. 重命名文件。
              3. 将这些文件移至您的主目录。
              4. 将源文件添加到 .bash_profile

                source ~/git-completion0.bash
                source ~/git-prompt0.sh
                and four to trigger the code block.
                

              【讨论】:

                【解决方案14】:

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

                我在 .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 上完美运行,而且很容易调整)。

                希望对你有帮助!

                【讨论】:

                  【解决方案15】:

                  我正在为 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

                  【讨论】:

                    【解决方案16】:

                    这个对我有用,它有彩色的 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/

                    【讨论】:

                      【解决方案17】:

                      对于 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
                      【解决方案18】:

                      我刚刚在 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
                      

                      我很高兴。

                      【讨论】:

                        【解决方案19】:

                        请注意,如果您没有通过 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

                        【讨论】:

                          【解决方案20】:
                          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
                          

                          【讨论】:

                          • 一个不错的改进可能是将您刚刚处于灰色状态的上一个分支添加到提示符中。
                          【解决方案21】:

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

                          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.

                          【讨论】:

                            猜你喜欢
                            • 2013-03-01
                            • 2013-10-12
                            • 2018-07-31
                            • 1970-01-01
                            • 2015-08-08
                            • 2014-10-10
                            • 2016-06-23
                            相关资源
                            最近更新 更多