1. 程式人生 > >Mac 下 git 命令自動補全

Mac 下 git 命令自動補全

一. 安裝 bash-completion

brew install bash-completion

安裝成功後提示:

==> Caveats
Add the following line to your ~/.bash_profile:
  [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

接下來按照提示,將下面語句放到~/.bash_profile

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

二. 下載 git-completion.bash

訪問 https://github.com/git/git
在這裡插入圖片描述
選擇 branch 為你 git 版本的分支,比如我的:

$ git version
git version 2.13.5 (Apple Git-94)

注意: 這裡如果不使用相匹配的版本,會導致後面 git 補全命令時報錯 Unknown option:

好,選好分支後,找到 contrib/completion/git-completion.bash

檔案,點選Raw,然後根據地址欄內容進行下載,存為 ~/.git-completion.bash

curl https://raw.githubusercontent.com/git/git/v2.13.5/contrib/completion/git-completion.bash -o ~/.git-completion.bash

修改 ~/.bash_profile

if [ -f ~/.git-completion.bash ]; then
    . ~/.git-completion.bash
fi

然後執行:

source ~/.bash_profile