如何配置Git Bash命令行自动补全?

245

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

我在http://git-scm.com/docs中没有找到任何东西,但如果我没记错的话,现在自动完成包含在git软件包中,我只需要在我的bashrc中添加正确的条目即可。


在我的 Ubuntu Precise(和 Fedora 17)上开箱即用。 - Mechanical snail
2
要检查您是否默认安装了它,可以运行(cd ~ && exec cat .bashrc | grep completion) - aderchox
4
你可以通过运行 grep completion ~/.bashrc 来检查它是否处于活动状态,而不需要使用 exec无用的 cat - jswetzen
17个回答

338

在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

sudo apt-get install git-core bash-completion

在Mac上

你可以使用Homebrew或者MacPorts安装git补全。

Homebrew

如果 $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

MacPorts

sudo port install git +bash_completion

然后将此添加到您的.bash_profile中:

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

在此指南中了解更多信息:安装 Bash git 自动补全

请注意,无论何种情况下,您都需要创建一个新的 shell(打开一个新的终端选项卡/窗口)才能使更改生效。


1
大多数*nix机器(特别是Ubuntu)已经有了那个文件,所以只需将其引用到我的用户即可解决我的问题。谢谢。 - nevvermind
6
Ubuntu可能将其放在/etc/bash_completion.d/git-prompt位置。 - Catskul
22
在我的 Ubuntu 14.04 中,该文件路径为 /usr/share/bash-completion/completions/git/etc/bash_completion.d/git-prompt 用于提供 git 提示支持,而不是补全。 - Rui Chen
28
警告:使用 Mac homebrew 方法只有在您已经通过 homebrew 安装了 git 的情况下才能生效。如果您之前是通过其他方式安装了 git,请执行 brew uninstall bash-completion 然后再执行 brew install git。以上步骤也适用于这种情况。 - patapouf_ai
1
要检查Bash版本,请键入echo $BASH_VERSION - Guglie
显示剩余8条评论

94

我也遇到了同样的问题,按照以下步骤进行:

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/


我在代理后面,所以必须先设置代理才能让curl工作。export https_proxy=proxy_ip:proxy_port 我真的不明白为什么它不能从系统中获取设置。 - madlymad
3
没有必要在文件名前面加点来隐藏它。此外,请注意Git版本(参见Wisbucky的答案)。 - Walter Tross
在Linux上,按照上述说明后,我需要退出终端并重新打开才能看到结果。此外,我还更改了curl URL中的git版本。 - Waqas
对于Mac:https://apple.stackexchange.com/a/92637/397202 - Sole Sensei

61

大多数指令都会告诉你下载

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

并在你的bash启动脚本中引用它,比如 .bashrc

但是这种方法存在问题,因为它引用的是master分支,也就是最新版本的git-completion.bash。问题在于有时它会因为不兼容安装的git版本而出错。

实际上,现在这种方法会失败,因为master分支的git-completion.bash新增了需要git v2.18的新特性,可是目前还没有哪个包管理器或安装程序升级到这个版本。你会收到一个错误信息:unknown option: --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

请注意URL中使用的版本号是v2.17.而不是master。当然,确保在bash启动脚本中引用它。

9
macOS 版本特定的文件应该已经在这里的磁盘上:/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash 为了确保安全,我将其与 Github 上的 2.17.1 版本进行了比较,并且匹配。 - jmt
在 Linux 上按照上述说明后,我需要退出终端并重新打开才能看到结果。 - Waqas

29

Ubuntu 14.10

安装 git-corebash-completion

sudo apt-get install -y git-core bash-completion
  • 对于当前会话使用:

source /usr/share/bash-completion/completions/git
始终在所有会话中开启它。
echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc

18

只需在您的 ~/.bashrc 文件中执行以下操作:

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

其他回答告诉你要安装 bash-completion,但其实不需要这么做。如果你真的想安装,那么就不需要直接使用源来完成。你只需要选择其中一种方法即可,不需要两种都用。

一个更通用的解决方案是按照 bash-completion 项目建议的查询系统位置。

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

通用解决方案在Void Linux上运行良好,尽管我必须先安装bash-completion。 - Adam J Richardson
bash-completion 应该会自动完成这个操作。如果你有 git bash-completion,但没有 bash-completion,则需要它。 - FelipeC

6

在我的Ubuntu系统中,有一个文件安装在这里:

source /etc/bash_completion.d/git-prompt

您可以跟随这些链接进入/usr/lib/git-core文件夹。您可以在那里找到有关如何设置PS1或使用__git_ps1的说明。


6

Xcode 开发人员工具安装 Git

在所有已发布的 macOS 的答案中,此方法只被提到了一次。这是由 jmt 在一个非常简短的评论中提到的...

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

相反,你只需要找到已经存在的 git-completion.bash 文件并将其源化在你的.bashrc文件中。请检查以下目录:

  • /Applications/Xcode.app/Contents/Developer/usr/share/git-core
  • /Library/Developer/CommandLineTools/usr/share/git-core

如果以上方法均无效,Git 本身也可以帮助你解决问题。当我执行以下命令时,Git 将返回一个设置,该设置来自于和我的git-completion.bash文件位于同一目录下的gitconfig文件:

$ 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上测试过)

2
这在我安装 Git for Windows 和 MSYS2,并希望在 MSYS2 下使用 Git 时也有效。在 Git Bash 中运行 find / -type f -name git-completion.bash 2>/dev/null 查找文件位置,然后引用它。然后在整个 completion 目录中使用 grep 命令查找 __git_ps1() 函数的位置,并将其引用。这样我就可以通过将 $PS1 环境变量从 Git Bash 复制到 MSYS2 Bash 中来获得提示符。 - Adrian

6
看起来很多人都来这里找出如何在他们的操作系统上启用选项卡自动完成。从一开始就需要注意到 git 的选项卡自动完成是与 shell 有关的,而不是与操作系统相关的。近年来,大多数 shell 工作在各种基于 Unix 的操作系统上,例如 bash, zsh, 或者 fish。只有少数几个适用于所有主要操作系统,例如 Powershell
如果你不确定你正在使用哪个 Unix shell,请尝试 echo $SHELL,或者查看 this question

Bash (大多数 Linux 发行版)

在大多数Unix发行版中,git的选项卡补全应该可以直接使用,安装在/etc/bash_completion.d//usr/share/bash-completion/completions/git。您可能正在寻找解决方法,因为它没有工作,这可能是因为补全脚本没有被使用。尝试将以下行添加到您的.bashrc文件中:
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git

然后在新的终端中再次尝试使用Tab键自动补全。
如果git自动补全功能出现问题或未安装,通过运行以下命令重新安装应该可以解决:
sudo apt-get install git-core bash-completion

再次,在新的终端中尝试使用Tab键自动补全。
要了解更多详情,请参阅git书籍附录A:其他环境下的Git - 在Bash中使用Git

在macOS上为bash用户安装git的Tab键自动补全

如果您在macOS上使用bash shell,可以使用Homebrew或MacPorts来安装git的自动补全功能。请参考@sergey-evstifeev的回答来进行操作。

Zsh(macOS和一些Linux发行版)

macOS和一些Linux发行版默认使用Zsh作为终端shell。要在Zsh中启用git的Tab键自动补全功能,请在您的.zshrc文件中添加以下内容:
autoload -Uz compinit && compinit

然后在新的终端中再次尝试使用制表符自动完成。

可选:考虑添加Oh My Zsh插件,以获取带有git信息的主题。

有关更多详细信息,请参阅git书籍附录A:其他环境中的Git - 在Zsh中使用Git

Powershell(Windows)

在Windows上,可以通过posh-git将制表符自动完成功能添加到Powershell中。(请注意,此功能不支持命令提示符)。

要在Powershell中安装posh-git,请执行以下操作:

脚本执行策略必须设置为RemoteSignedUnrestricted。通过执行Get-ExecutionPolicy命令来检查脚本执行策略设置。如果策略没有设置为所需的两个值之一,以管理员身份运行 PowerShell 并执行Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
然后使用 PowerShellGet 安装。
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force

然后在新的终端中再次尝试制表符自动完成。

有关Chocolatey、Scoop或手动安装说明,请参阅posh-git GitHub页面

可选:还可以考虑升级到Powershell 7,它具有用于智能制表符自动完成的命令行智能感知功能。

有关更多详细信息,请参阅git书籍附录A:Powershell中的其他环境中的Git


3

也许对某些人有帮助:--

从以下链接下载.git-completion.bash文件后,

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

当我尝试使用__git_ps1函数时,遇到了错误--

 -bash: __git_ps1: command not found

显然,我们需要从主文件中单独下载脚本以使此命令生效,因为__git_ps1是在git-prompt.sh中定义的。所以类似于下载.git-completion.bash一样,获取git-prompt.sh:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git

然后在您的.bash_profile中添加以下内容:

source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi

source ~/.bash.git 将会执行下载的文件。

export PS1='\W$(__git_ps1 "[%s]") 命令将在当前工作目录后附加检出的分支名称(如果它是一个 git 存储库)。

因此,它会像这样:

dir_Name[branch_name] 其中 dir_Name 是工作目录名称,branch_name 是您当前正在使用的分支的名称。

请注意 - __git_ps1 区分大小写。


2

Windows

在 Windows 11 的命令提示符(CMD)中,以下是我最终的解决方案:

  • 安装 Clink(为 Windows 提供 Bash 功能)
  • git-autocomplete.lua 文件复制到 C:\Users\<username>\AppData\local\clink 目录中

链接的 clink 已不再维护。现在可使用维护的分支:https://chrisant996.github.io/clink/。已提交更新存储库中完成脚本链接的 PR:https://github.com/ztomm/git-autocomplete-for-windows/pull/1 - koppor
1
@koppor 谢谢。我已经更新了链接。显然,新版本的Windows不再需要重新启动。 - ztom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接