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

-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

当前回答

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

其他回答

这在OS 10.8的.bash_profile中工作

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

升级到OSX 10.9 Mavericks后,我不得不引用以下文件来获得git shell命令补全和git提示符。

从我的.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)")\$ '

请不要这样,如果你没有通过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

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

拷贝/下载以下文件并复制到主目录:~/

git-completion.bash

git-prompt.sh

对于bash_profile,在开头添加这个:

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

想要更多更简单的下载,你可以检查这个。