在 Linux 上
在大多数发行版上,安装 git 时 git 完成脚本会安装到 /etc/bash_completion.d/(或 /usr/share/bash-completion/completions/git)中,无需去 github。您只需要使用它 - 将此行添加到您的 .bashrc:
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git
在某些版本的 Ubuntu 中,git 自动完成功能可能默认被破坏,通过运行此命令重新安装应该可以修复它:
sudo apt-get install git-core bash-completion
在 Mac 上
您可以使用 Homebrew 或 MacPorts 安装 git 补全。
自制
如果$BASH_VERSION > 4:brew install bash-completion@2(更新版本)
请特别注意您拥有的 bash 版本,因为 MacOS 默认随附 3.2.57(1)-release。
添加到.bash_profile:
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
对于旧版本的 bash:brew install bash-completion
添加到.bash_profile:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
Mac 端口
sudo port install git +bash_completion
然后将其添加到您的.bash_profile:
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
本指南中的更多信息:Install Bash git completion
请注意,在所有情况下,您都需要创建一个新的 shell(打开一个新的终端选项卡/窗口)才能使更改生效。