我正在尝试更改终端中的命令提示符。我总是得到错误:

-bash: __git_ps1:命令未找到

我已经尝试过了,只是把它输入到终端,就像:__git_ps1。我还在.bash_profile中尝试了它

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

正如你可能会看到/告诉,是的,我确实安装了自动完成,它确实工作得很好!

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

别名__git_ps1 =“git分支2 > / dev / null | grep‘*’| sed /* \(.*\)/(\ 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):没有这样的文件或目录

注意:我也将别名移到源代码下面,没有任何区别。我的git版本是1.7.12.1

这应该是一个简单的更改。有人能帮帮我吗?

编辑10/13/12

不,我肯定不想自己定义__git_ps1,但只是想看看这样做是否会被识别。是的,我有。git完成。已安装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完成符。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

当前回答

以下内容对我来说非常有效:

在终端运行以下命令:

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:命令找不到的问题。

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

其他回答

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

我知道这不是真正的答案……

我在我的.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上工作完美,很容易调整)。

我希望这能有所帮助!

我升级到约塞米蒂时也遇到了同样的问题。

我只需要修改~/。Bashrc到source /usr/local/etc/ bash_complete .d/git-prompt.sh,而不是原来的路径。

然后资源。~ /。Bashrc得到的效果。

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

高Sierra清洁解决方案与颜色!

没有下载。没有啤酒。没有Xcode

只需将其添加到~/。bashc或~/.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" "\\\$ "'