Mac下Git命令自动补全

Quick Start

一 、安装 bash-completion

1
2
3
4
5
6
7
8
9
$ 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(如果没有该文件,新建一个)
重启终端

二、拷贝文件、设置路径

访问网站 https://github.com/git/git.git
找到 ”contrib/completion/” 目录下的git-completion.bash ,
然后点击编辑,拷贝其内容,复制到文本文件,保存为 git-completion.bash 文件
然后将文件用命令拷贝到 ~/ 目录下

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

xxx 为文件所在目录,注意拷贝后的文件名称为 .git-completion.bash

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

1
$ source ~/.git-completion.bash

三、 启动: 终端输入

1
$ source ~/.git-completion.bash

将下面这句话 添加到~/.bash_profile

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

不添加想要补全起作用,每次都需要执行
source ~/.git-completion.bash
补全才能生效