我正在尝试在终端中更改我的命令提示符。我不断收到错误:
-bash: __git_ps1: command not found
我只是通过在终端中输入它来尝试它:__git_ps1
。我也在 .bash_profile
中尝试过
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
正如您可能看到/告诉的那样,是的,我确实安装了自动完成功能,而且效果很好!
我遇到了这个问题:“PS1 env variable does not work on mac”给出了代码
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\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): No such file or directory
注意:我还将别名移到源下方,没有任何区别。我有 git 版本 1.7.12.1
这应该是一个简单的改变。有人可以帮帮我吗?
2012 年 10 月 13 日编辑
不,我绝对不想自己定义 __git_ps1 ,只是想看看这样做是否会被识别。是的,我已经安装了 .git-completion.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-completion.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-completion.bash
将 __git_ps1
定义为 shell 函数。你的呢?你怎么知道完成是有效的?当您键入 git h
git <tab>
来启用提示,如 Mark Longair 引用的提交中所述。
您已经从 master
安装了 git-completion.bash
的版本 - 在 git 的开发历史中,这是在将 __git_ps1
函数从完成功能拆分为新文件 (git-prompt.sh
) 的提交之后。引入此更改并解释其基本原理的提交是 af31a456。
我仍然建议您只获取与您的 git 安装捆绑在一起的 git-completion.bash
(或 git-prompt.sh
)版本。
但是,如果出于某种原因您仍想通过使用从 master
单独下载的脚本来使用此功能,您应该类似地下载 git-prompt.sh
:
curl -o ~/.git-prompt.sh \
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
...并将以下行添加到您的 ~/.bash_profile
:
source ~/.git-prompt.sh
那么包含 __git_ps1 '%s'
的 PS1
变量应该可以正常工作。
升级到 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)")\$ '
~/.git_prompt.sh
。其他解决方案不起作用。
bash: __docker_machine_ps1: command not found
你应该
brew install bash bash-completion git
然后在 .bashrc 中获取“$(brew --prefix)/etc/bash_completion”。
以下对我来说就像一个魅力:
在终端中运行以下命令:
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: command not found
的问题。
您的提示也将更改为“os”。要将“os”更改为其他内容,请修改导出 PS1 行中的“os”字符串。
MacOS Sierra 和 git 版本 2.10.1 的解决方案 <2017-2-06>
第 1 步:安装 Git
如果您已经安装了最新的 git,则可以跳过此步骤。
从浏览器下载 git 包 https://git-scm.com/download/
注意:如果您安装时使用 curl [option] https://...
选项下载,您必须确保您的系统支持 SSL。所以对于新手来说,从浏览器下载并直接从 git 安装程序安装要容易得多。
显示你的 git 目录在哪里:which git
显示你的 git 当前是哪个版本: git --version 当前版本应该是 2.10.1。
第 2 步:将您的 git 配置文件添加到您的 shell
打开你的 shell 配置文件: nano ~/.bash_profile 或 nano ~/.bashrc 取决于你的修改在哪里。将以下代码添加到文件中: source /usr/local/git/contrib/completion/git-completion.bash source /usr/local/git/contrib/completion/git-prompt.sh
注意:在 OSX 升级到 El Capitain 后,git 安装位置从 opt/ 目录更改为 usr/local/,这就是为什么上面的一些旧答案在 MacOS Sierra 中不再适用的原因。
将以下代码添加到您的 PS1 配置中: 选项 1:直接添加到您的 PS1:export PS1='\w$(__git_ps1 "(%s)") > ' 我更喜欢这种简单的方法,因为我已经知道 .git-completion .bash 在我的主目录中,我可以在它的前面添加其他提示格式。这是我个人的提示供您参考:export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> ' 选项2:添加选择脚本 if [ -f ~/.git-completion.bash ];然后 export PS1='\w$(__git_ps1 "(%s)") > ' fi 保存并使用配置文件: source ~/.bash_profile 或 source ~/.bashrc
带有颜色的 High Sierra 清洁解决方案!
没有下载。没有酿造。没有 Xcode
只需将其添加到您的 ~/.bashrc 或 ~/.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" "\\\$ "'
升级到优胜美地时我遇到了同样的问题。
我只需要将 ~/.bashrc
修改为 source /usr/local/etc/bash_completion.d/git-prompt.sh
而不是旧路径。
然后重新获取您的 . ~/.bashrc
以获得效果。
bash 的 __git_ps1 现在在我的 brew 安装的 git 版本 1.8.1.5 的 /usr/local/etc/bash_completion.d 的 git-prompt.sh 中找到
这适用于 .bash_profile 中的 OS 10.8
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='YOURNAME[\W]$(__git_ps1 "(%s)"): '
fi
对于 macports,我必须将:source /opt/local/share/git-core/git-prompt.sh
添加到我的 ./profile
touch ~/.profile
。但你显然需要一个文件:/opt/local/share/git-core/git-prompt.sh
如果您希望使用 Homebrew 升级 Git,并且您的系统总体上已经过时(就像我所做的那样),您可能需要先将 Homebrew 本身更新(根据 {1 } 感谢@chris-frisina)
首先使 Homebrew 与当前版本保持一致
cd /usr/local git fetch origin git reset --hard origin/master
然后更新 Git:
酿造升级 git
问题解决了! ;-)
至少在 Xcode 6 中,您已经拥有 git-completion.bash
。它在 Xcode 应用程序包中。
只需将其添加到您的 .bashrc 中:
source `xcode-select -p`/usr/share/git-core/git-completion.bash
从此 Git 完成下载文件 git-prompt.sh 和 git-completion.bash 重命名文件。将这些文件移动到您的主目录。将源文件添加到 .bash_profile 源 ~/git-completion0.bash 源 ~/git-prompt0.sh 和四个以触发代码块。
我知道这不是一个真正的答案...
我在 .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 上完美运行并且很容易调整)。
我希望它有帮助!
我正在为 git hub 上 Udacity 的课程,并且遇到了同样的问题。这是我的最终代码,它可以正常工作。
# Change command prompt
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
source ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
有用! https://i.stack.imgur.com/d0lvb.jpg
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/
对于 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
bash: __docker_machine_ps1: command not found
我刚刚在 Mojave 上安装的另一个选项:magicmonty/bash-git-prompt
运行 (brew update
) 和 brew install bash-git-prompt
或 brew install --HEAD bash-git-prompt
然后到您的 ~/.bash_profile
或 ~/.bashrc
:
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
GIT_PROMPT_ONLY_IN_REPO=1
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
我很高兴。
请注意,如果您没有通过 Xcode 或 home-brew 安装 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
复制/下载以下文件并将它们复制到主目录:~/
对于 bash_profile,在开头添加:
source ~/git-completion.bash
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
For more and easy downloads you could check this。
不定期副业成功案例分享
git-prompt.sh
和git-completion.bash
都在`brew --prefix git`/etc/bash_completion.d/
中。git-core
被git
替换时,路径发生了变化,请参阅 this question