如何讓 git-completion.bash 在 Mac OS X 上運作?

如何讓 git-completion.bash 在 Mac OS X 上運作?

我已關注http://blog.bit Fluent.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

你能告訴我我錯過了什麼嗎?

答案1

我使用安裝了 gitMac埠在我新安裝的 Snow Leopard 上。從 .dmg 映像安裝 MacPorts 後,Terminal.app 中的命令如下:

sudo port selfupdate
sudo port install git-core +bash_completion

如果您還需要支援從 SVN 儲存庫和文件中提取數據,請使用此程式碼而不是第二行:

sudo port install git-core +bash_completion +doc +svn

然後將以下內容加入您的 ~/.profile 或 ~/.bash_profile 中:

# MacPorts Bash shell 指令完成
如果 [ -f /opt/local/etc/bash_completion ];然後
    。 /opt/local/etc/bash_completion

或對於 Mountain Lion 上 2.1.2 版本之後的 MacPort:

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

或對於具有較新版本 git 的 MacPort:

如果 [ -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 bash bash,然後再嘗試 git 補全。

答案2

如果您使用 homebrew 安裝了 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

檢查是否正確安裝 git 的最佳方法是使用 homebrew ist 執行

brew info git

並檢查 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

答案3

您需要做的就是將該git-completion.bash文件放入您的用戶主bin目錄中,並將以下內容放入您.profile.bash_profile文件中:

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

這樣做的作用是確保您的本地 bin 位於 PATH 中,並且命令source可以讓事情順利進行。當然,PS1 變更會將目前簽出的分支放入提示中。

因此,無需安裝 MacPort 即可安裝 GIT 的「完整」版本(如果您已經安裝了它,則尤其令人惱火)。

答案4

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

. /opt/local/etc/bash_completion.d/git-completion

相關內容