Mac下git命令自动补全

当我第一次在mac上安装git,【tab】补全装成功了,但是我没有记录,当我过一段时间在重装的时候,我已经忘记了,又是各种查资料,再次做一下简单的记录。

首先,我因为还是mac小白,所以使用Homebrew安装的git,如何安装Honebrew及其使用在此就跳过,烦请自行查看(http://brew.sh/index_zh-cn.html),直接进入正题:

$ brew list

查看你是否已经安装了"bash-completion",如果没有,继续往下看:

$ brew install bash-completion
#####安装完成之后######
$ brew info bash-completion  
#####下边这句话很重要#######
==> Caveats
Add the following lines to your ~/.bash_profile:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi

将if...then...那一句添加到~/.bash_profile(如果没有该文件,新建一个)

重启终端,以上为安装bash-completion部分。

接下来将git源码clone到本地

$ git clone https://github.com/git/git.git

找到"contrib/completion/"目录下的git-completion.bash,将该文件拷贝到~/下并重命名为.git-completion.bash

$ cp git-completion.bash ~/.git-completion.bash

在~/.bashrc文件(该目录下如果没有,新建一个)中添加下边的内容

source ~/.git-completion.bash

好了,重启终端以后就大功告成了

$ git --h[tab][tab]
--help        --html-path
原文地址:https://www.cnblogs.com/yangfei/p/3627442.html