简体   繁体   English

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

[英](Mac) -bash: __git_ps1: command not found

I'm trying to change my command promt in terminal.我正在尝试在终端中更改我的命令提示符。 I keep getting the error:我不断收到错误:

-bash: __git_ps1: command not found

I've tried it just by typing it into the terminal as is: __git_ps1 .我只是通过在终端中按__git_ps1输入它来尝试它: __git_ps1 I've also tried it out in the .bash_profile我也在.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)/'"

So I add it to my .bash_profile hoping that it will change something.所以我将它添加到我的.bash_profile希望它会有所改变。 Well, it did.嗯,确实如此。 It just changed the error output.它只是改变了错误输出。

Here's the .bash_profile with the addition:这是.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

And now here's the changed error output:现在这是更改后的错误输出:

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.不,我绝对不想自己定义 __git_ps1 ,只是想看看这样做是否会被识别。 Yes, I have the .git-completion.bash file installed.是的,我已经安装了.git-completion.bash文件。 Here's how I got auto completion on my machine.这是我如何在我的机器上自动完成。

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

A ls -la then lists the .git-completion.bash file. ls -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_profile while others did not...以下代码在.bash_profile对我.bash_profile而其他代码则没有...

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

You've installed the version of git-completion.bash from master - in git's development history this is after a commit that split out the __git_ps1 function from the completion functionality into a new file ( git-prompt.sh ).您已经从master安装了git-completion.bash版本 - 在 git 的开发历史中,这是在将__git_ps1函数从完成功能拆分为一个新文件( git-prompt.sh )的提交git-prompt.sh The commit that introduced this change, which explains the rationale, is af31a456 .引入此更改的提交(解释了基本原理)是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-prompt.sh与您的 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.sh similarly:但是,如果出于某种原因您仍然希望通过使用从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 PS1 variable that includes __git_ps1 '%s' should work fine.那么包含__git_ps1 '%s' PS1变量应该可以正常工作。

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

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

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='\[\033[01;32m\]os \[\033[01;34m\]\w $(__git_ps1 "[%s]")\$\[\033[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 ".您的提示也将更改为“os”。 To change "os" to something else, modify "os" string in export PS1 line.要将“os”更改为其他内容,请修改 export PS1 行中的“os”字符串。

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/从浏览器下载 git 包https://git-scm.com/download/

Note: if you install with curl [option] https://... option to download, you would have to make sure your system support SSL.注意:如果您使用curl [option] https://...下载选项进行安装,您必须确保您的系统支持 SSL。 So for new comer, to download from browser and install directly from git installer is much easier.所以对于新手来说,从浏览器中下载并直接从 git installer 安装要容易得多。

Installation Verification: 安装验证:
  • Show where is your git directory at: which git显示你的 git 目录在哪里: which git
  • Show which version your git currently is: git --version current version should be 2.10.1.显示您的 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_profile or nano ~/.bashrc Depends on where your modification is. nano ~/.bash_profilenano ~/.bashrc取决于您的修改位置。
  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

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:将以下代码添加到您的 PS1 配置中:

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

      I prefer this simple approach since I already know the .git-completion.bash is there in my home directory, and I can add other prompt format in the front of it.我更喜欢这种简单的方法,因为我已经知道.git-completion.bash在我的主目录中,我可以在它前面添加其他提示格式。 here is my personal prompt for your reference: export PS1='\\t H#\\! \\u:\\w$(__git_ps1 "{%s}") -->> '这是我的个人提示供您参考: export PS1='\\t H#\\! \\u:\\w$(__git_ps1 "{%s}") -->> ' export PS1='\\t H#\\! \\u:\\w$(__git_ps1 "{%s}") -->> '
    • Option 2: Add a selection script选项 2:添加选择脚本

    if [ -f ~/.git-completion.bash ];如果 [ -f ~/.git-completion.bash ]; then然后\n      export PS1='\\w$(__git_ps1 "(%s)") > '导出 PS1='\\w$(__git_ps1 "(%s)") > '\nfi
  2. Save and use the profile: source ~/.bash_profile or source ~/.bashrc保存并使用配置文件: source ~/.bash_profilesource ~/.bashrc

Now you should see the git prompt working properly and shows which branch you are in right now. 现在您应该看到 git 提示正常工作并显示您现在所在的分支。

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

I had same problem when upgrading to Yosemite.升级到优胜美地时我遇到了同样的问题。

I just had to modify ~/.bashrc to source /usr/local/etc/bash_completion.d/git-prompt.sh instead of the old path.我只需要修改~/.bashrc以获取source /usr/local/etc/bash_completion.d/git-prompt.sh而不是旧路径。

then re-source your . ~/.bashrc然后重新获取您的. ~/.bashrc . ~/.bashrc to get the effect. . ~/.bashrc得到效果。

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

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

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

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 cd /usr/本地
git fetch origin git 获取来源
git reset --hard origin/master git reset --hard origin/master

Then update Git:然后更新 Git:

brew upgrade git酿造升级git

Problem Solved!问题解决了! ;-) ;-)

At least with Xcode 6, you already have git-completion.bash .至少在 Xcode 6 中,你已经有了git-completion.bash It's inside the Xcode app bundle.它位于 Xcode 应用程序包内。

Just add this to your .bashrc:只需将其添加到您的 .bashrc 中:

source `xcode-select -p`/usr/share/git-core/git-completion.bash
  1. Download the files git-prompt.sh and git-completion.bash from this Git completion从此Git 完成下载文件git-prompt.shgit- completion.bash
  2. Rename the files.重命名文件。
  3. Move those files to your home directory.将这些文件移动到您的主目录。
  4. Add the source file in to the .bash_profile将源文件添加到 .bash_profile

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

I know it's not a real answer...我知道这不是一个真正的答案......

I had some strange issues with sourcing git-prompt.sh in my .bashrc so I started to look for other solution.我在 .bashrc 中获取 git-prompt.sh 时遇到了一些奇怪的问题,所以我开始寻找其他解决方案。 This one: http://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ doesn't use __git_ps1 and author claims it works also on Mac (for now it works perfectly on my Ubuntu and it's easy to tweak).这个: http ://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ 不使用 __git_ps1 并且作者声称它也有效在 Mac 上(目前它在我的 Ubuntu 上运行良好,而且很容易调整)。

I hope it helps!我希望它有帮助!

I was doing the course on Udacity for git hub and was having this same issue.我正在为 git hub 开设有关 Udacity 的课程,并且遇到了同样的问题。 Here is my final code that make is work correctly.这是我的最终代码,使 make 正常工作。

# 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"

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

This one worked for me, and it has colored git output and an indicator in the prompt whether files have changed / been added, right baked into it:这个对我有用,它对 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\]\$ '

Be sure to use the correct path!一定要使用正确的路径! I used homebrew to install git, use brew list git to get the path to your current installation.我使用 homebrew 安装 git,使用brew list git获取当前安装的路径。

Would be nice not to use a hard coded path, but don't know how to get the path to the current installation.最好不要使用硬编码路径,但不知道如何获取当前安装的路径。

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

For git, there are /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh .对于 git,有/Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh And please look /etc/bashrc_Apple_Terminal too.也请查看/etc/bashrc_Apple_Terminal

So, I put these in ~/.bash_profile :所以,我把这些放在~/.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

Please not that, if you haven't installed git through Xcode or home-brew, you'll likely find the bash scripts haysclarks refers to in /Library/Developer/CommandLineTools/ , and not in /Applications/Xcode.app/Contents/Developer/ , thus making the lines to include within .bashrc the following:请注意,如果您没有通过 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

You'll need those lines if you wish to use git-prompt as well.如果您还想使用 git-prompt,您将需要这些行。 [1]: https://stackoverflow.com/a/20211241/4795986 [1]: https : //stackoverflow.com/a/20211241/4795986

Yet another option I just installed on Mojave: magicmonty/bash-git-prompt我刚刚在 Mojave 上安装的另一个选项: magicmonty/bash-git-prompt

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

Then to your ~/.bash_profile or ~/.bashrc :然后到您的~/.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

I'm happy.我很高兴。

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

Copy/Download the following files and copy them to home directory: ~/复制/下载以下文件并将它们复制到主目录: ~/

git-completion.bash git-completion.bash

git-prompt.sh git-prompt.sh

For the bash_profile, add this at the beginning:对于 bash_profile,在开头添加:

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

For more and easy downloads you could check this .如需更多和简单的下载,您可以查看此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM