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

-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

当前回答

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

只要把这个添加到你的.bashrc中:

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

其他回答

你应该

$ brew安装bash bash完成git

然后在你的.bashrc中输入“$(brew——prefix)/etc/bash_completion”。

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

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

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

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

cd /usr/local Git获取来源 Git重置-硬源/主

然后更新Git:

Brew升级git

问题解决了!: -)

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