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

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

其他回答

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

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

在终端运行以下命令:

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有/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

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

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

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

你应该

$ brew安装bash bash完成git

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