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

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


当前回答

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中测试)

其他回答

窗户

我最终在Windows 10命令行(cmd)上的工作原理:

安装叮当声 git-autocomplete副本。lua文件到C:\Users\<username>\AppData\local\clink目录 重新启动Windows

Ubuntu 14.10

安装git-core和bash-completion

sudo apt-get install -y git-core bash-completion

对于当前会话使用情况 源/usr/share/bash-completion /完成/ git 在所有会议上都开着 Echo "source /usr/share/bash-completion/completion /git" >> ~/.bashrc

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中测试)

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

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

在我的ubuntu上有一个文件安装在这里:

source /etc/bash_completion.d/git-prompt

您可以按照链接进入/usr/lib/git-core文件夹。你可以在那里找到一个指令,如何设置PS1或使用__git_ps1