例如,在一台新的ubuntu机器上,我刚刚运行了sudo apt-get git,在输入例如git check[tab]时没有完成。

我在http://git-scm.com/docs上没有找到任何东西,但IIRC完成是包含在这些天的git包中,我只需要在我的bashrc中正确的条目。


当前回答

在Linux上

在大多数发行版中,git完成脚本安装在/etc/bash_completion中。D /(或/usr/share/bash-completion/completion /git)安装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完成。

家酿

if $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安装bash-completion

添加到.bash_profile:

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

MacPorts

Sudo端口安装git +bash_completion

然后把这个添加到你的.bash_profile中:

if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
fi

更多信息在本指南:安装Bash git完成

注意,在所有情况下,您都需要创建一个新的shell(打开一个新的终端选项卡/窗口),以便更改生效。

其他回答

在Github的Git项目中,他们提供了一个bash文件来自动完成Git命令。

您应该将其下载到主目录,并强制bash运行它。这只是简单的两个步骤,并在接下来的博客文章中完美地解释(一步一步地)。

代码担忧者博客:autocomplete-git/

我已经在mac上测试过了,它应该也能在其他系统上工作。您可以将相同的方法应用于其他操作系统。

参见https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

您只需要获取完成脚本的源代码

在Linux上

在大多数发行版中,git完成脚本安装在/etc/bash_completion中。D /(或/usr/share/bash-completion/completion /git)安装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完成。

家酿

if $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安装bash-completion

添加到.bash_profile:

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

MacPorts

Sudo端口安装git +bash_completion

然后把这个添加到你的.bash_profile中:

if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
fi

更多信息在本指南:安装Bash git完成

注意,在所有情况下,您都需要创建一个新的shell(打开一个新的终端选项卡/窗口),以便更改生效。

macOS通过Xcode开发工具

在目前发布的所有macOS的答案中,这只在jmt的一个非常简短的评论中提到…

如果你已经安装了Xcode开发工具,那么你不需要下载任何新工具。

相反,您只需要定位已经存在的git-completion。Bash文件并在.bashrc中获取它。检查以下目录:

/应用程序/ xcode /内容/开发/ usr / share / git-core /图书馆/开发/ CommandLineTools / usr / share / git-core

如果做不到这一点,git本身可能能够帮助您。当我像下面这样运行git配置时,git报告了一个来自gitconfig文件的设置,该文件与我的git-completion.bash位于同一目录:

$ git config --show-origin --list
...
file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig    credential.helper=osxkeychain
...

或者你也可以在你的机器上用蛮力搜索,然后买杯咖啡:

$ find / -type f -name git-completion.bash 2>/dev/null

因此,我在~/.bashrc中插入了以下内容:

# Git shell completion and prompt string on macOS
_git_dir="/Applications/Xcode.app/Contents/Developer/usr/share/git-core"
if [ -f "${_git_dir}/git-completion.bash" ]; then
    source "${_git_dir}/git-completion.bash"
fi
if [ -f "${_git_dir}/git-prompt.sh" ]; then
    source "${_git_dir}/git-prompt.sh"
fi
unset _git_dir

注意,这也是git提示字符串脚本的来源,因为它位于同一个目录中。

(在macOS Catalina中测试)

只需在~/.bashrc中这样做:

source /usr/share/bash-completion/completions/git

其他答案告诉你安装bash-completion,你不需要这样做,但如果你这样做,那么就不需要直接源完成。你只能做其中一个,不能两个都做。

一个更通用的解决方案是查询bash-completion项目推荐的系统位置:

source "$(pkg-config --variable=completionsdir bash-completion)"/git