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

-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

当前回答

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

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

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

然后更新Git:

Brew升级git

问题解决了!: -)

其他回答

你应该

$ brew安装bash bash完成git

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

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

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

我希望这能有所帮助!

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

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

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

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

请不要这样,如果你没有通过Xcode或自制安装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