在 Mac 中,使用 bash-completion 进行git命令自动补全。
安装 bash-completion
# 查看是否已经安装。
$ brew list
# 若未安装,请执行
$ brew install bash-completion
安装完成后,信息如下:
$ brew info 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 版本的 tag
分支,比如:
$ git version
git version 2.11.0 (Apple Git-81)
注意:
请使用相匹配的版本,否则会导致 git 命令补全时报错 Unknown option:
选好分支后,找到 contrib/completion/git-completion.bash
文件,点击Raw,复制地址栏的地址。
$ cd /usr/local/opt/bash-completion/etc/bash_completion.d
$ curl -L -O https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
最后,退出终端重新打开。