如何让 git-completion.bash 在 Mac OS X 上工作?

n17*_*911 93 mac bash git macos

我已经按照http://blog.bitfluent.com/post/27983389/git-utilities-you-cant-live-without添加git-completion.bash到我的/opt/local/etc/bash_completion.d/git-completion

然后我输入PS1='\h:\W$(__git_ps1 "(%s)") \u\$ '了我的 .bashrc_profile

但现在我得到了-bash: __git_ps1: command not found我所做的一切cd

你能告诉我我错过了什么吗?

Lar*_*gan 64

我在新安装的 Snow Leopard 上使用MacPorts安装了 git 。从 .dmg 映像安装 MacPorts 后,这些将是 Terminal.app 中的命令:

sudo port selfupdate
sudo port install git-core +bash_completion
Run Code Online (Sandbox Code Playgroud)

如果您还希望支持从 SVN 存储库和文档中提取,请使用此而不是第二行:

sudo port install git-core +bash_completion +doc +svn
Run Code Online (Sandbox Code Playgroud)

然后将以下内容添加到您的 ~/.profile 或 ~/.bash_profile 中:

# MacPorts Bash shell 命令完成
如果 [ -f /opt/local/etc/bash_completion ]; 然后
    . /opt/local/etc/bash_completion
菲

或者对于 Mountain Lion 上 2.1.2 版以后的 MacPorts:

# MacPorts Bash shell 命令完成
如果 [ -f /opt/local/etc/profile.d/bash_completion.sh ]; 然后
  . /opt/local/etc/profile.d/bash_completion.sh
菲

或者对于带有较新版本 git 的 MacPorts:

如果 [ -f /opt/local/share/git-core/git-prompt.sh ]; 然后
    . /opt/local/share/git-core/git-prompt.sh
菲

注意:bash_completion.sh 需要 bash 4.1 或更高版本。如果完成不起作用echo $BASH_VERSION,请尝试查看是否是问题所在。如果是这样,请输入 MacPorts bashbash并再次尝试 git completion。

  • 因为`/opt/local/etc/profile.d/bash_completion.sh` 需要`$BASH_VERSION` 4.1 或更高版本,我必须在带有 MacPorts 2.1.3 的 Mac OS X 10.8.3 上执行以下操作: 切换 Mac OS X 登录 shell 从其默认的 `/bin/bash` (3.2.48(1)-release) 到 MacPorts `/opt/local/bin/bash` (4.2.42(2)-release) 就像这里描述的那样:stackoverflow .com/a/791244/287984。基本上``if [ `grep /opt/local/bin/bash /etc/shells` ]; 然后 echo /opt/local/bin/bash | chsh -s /opt/local/bin/bash; else echo /opt/local/bin/bash | 须藤 tee -a /etc/shells; chsh -s /opt/local/bin/bash; 菲` (3认同)

小智 57

如果您使用自制软件安装了 git,那么您可能会稍微调整 MacPorts 建议并将其添加到您的.bash_profile.bashrc

if [ -f `brew --prefix`/etc/bash_completion.d/git-completion.bash ]; then
. `brew --prefix`/etc/bash_completion.d/git-completion.bash
fi
Run Code Online (Sandbox Code Playgroud)

检查是否正确安装了 git 的最佳方法是使用 homebrew ist 来执行

brew info git
Run Code Online (Sandbox Code Playgroud)

并检查 git bash 完成的安装目录的输出

最新版本的 Git (1.7.12) 还需要以下内容来启用提示。

if [ -f `brew --prefix`/etc/bash_completion.d/git-prompt.sh ]; then
    . `brew --prefix`/etc/bash_completion.d/git-prompt.sh
fi
Run Code Online (Sandbox Code Playgroud)


小智 26

您需要做的就是将git-completion.bash文件放在您的用户主bin目录中,并将以下内容放入您.profile.bash_profile文件中:

export PATH="$HOME/bin:$PATH"
source ~/bin/git-completion.bash
PS1='[\u@\h \w$(__git_ps1 " (%s)")]\$ '
Run Code Online (Sandbox Code Playgroud)

这样做的目的是确保您的本地 bin 位于 PATH 中,并且该source命令使事情顺利进行。然后当然 PS1 更改将当前签出的分支放在提示中。

因此,无需安装 MacPort 即可安装 GIT 的“完成”版本(如果您已经安装了它,则尤其令人恼火)。

  • https://github.com/git/git/raw/master/contrib/completion/git-completion.bash (2认同)
  • 对于那些想知道如何将 PS1 内容与提示中的颜色相结合的人,请参阅:http://stackoverflow.com/questions/816790/append-gits-branch-name-to-command-prompt (2认同)

小智 13

虽然当前的答案对于较旧的 MacPorts 是正确的,但最新的 MacPorts 存在一个新问题,导致相同的错误。

当前的bash 完成包 (2.0 r1) 至少需要 bash 4.1 才能正常运行。作为通常的 OS X 终端使用/bin/bash它只能获得 3.x。您必须按照/opt/local/bin/bash -l此处的 MacPorts 文档中的说明更改终端设置以使用:http : //trac.macports.org/wiki/howto/bash-completion


jti*_*man 6

您需要获取命令完成功能。在 PS1 之前添加到您的 .bashrc_profile 中:

. /opt/local/etc/bash_completion.d/git-completion
Run Code Online (Sandbox Code Playgroud)


stu*_*eek 5

令人讨厌的是,这又被打破了。这次是版本 1.7.12-1 的 git 人员。为了让人们在没有完成功能的情况下只拥有提示功能(这会减慢速度),他们将提示功能从 bash_completion/git 重构到了自己的文件 git-prompt.sh 中。

因此,按照@Lara 的描述添加 bash_completion 只会让您完成(点击选项卡完成标签/分支名称)。

要获得它__git_ps1以便您可以在 PS1 提示中使用它,您还需要将以下内容添加到您的 ~/.profile。

. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh

一些背景在这里 - https://bbs.archlinux.org/viewtopic.php?pid=1163632#p1163632