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

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


当前回答

你看到的大多数说明都会告诉你如何下载

https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

在你的bash启动脚本中,比如。bashrc。

但这有一个问题,因为它引用的是主分支,这是git-completion.bash的最新版本。问题是有时它会崩溃,因为它与你安装的git版本不兼容。

事实上,现在它将被打破,因为主分支的git-completion。Bash有需要git v2.18的新特性,包管理器和安装程序还没有更新到git v2.18。你会得到一个错误未知选项:——list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config

因此,最安全的解决方案是引用与您安装的git匹配的版本/标记。例如:

https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash

注意,它有一个v2.17版本。而不是master。然后,当然,要确保在bash启动脚本中提供源代码。

其他回答

我也有同样的问题,遵循以下步骤:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

然后将以下行添加到.bash_profile(通常在您的主文件夹下)

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

来源:http://code-worrier.com/blog/autocomplete-git/

在Ubuntu 22.04中,只需在.bashrc或.zshrc后面添加这一行即可

source /etc/bash_completion.d/git-prompt

Ubuntu

这里有一个美丽的答案。在Ubuntu 16.04上为我工作

窗户

Git Bash是允许自动完成的工具。不确定这是否是标准发行版的一部分,所以你可以发现这个链接也很有用。 顺便说一下,Git Bash允许使用Linux shell命令在windows上工作,这对于有GNU/Linux环境经验的人来说是一件很棒的事情。

Mac M1

对于那些使用Mac M1环境的用户,我可以通过自制软件进行安装:

酿造安装bash-completion

然后添加到我的~/。Bash_profile或~/。Bashrc(不管你用什么):

[[ -r "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh"

您可能需要更新版本号(1.3_3)。你只需要在那个目录中查找即可。我很想知道有没有更好的办法。

窗户

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

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