简体   繁体   English

如何使用Git启用自动完成功能[所有平台]

[英]How to enable auto-completion with Git [all platforms]

How to enable auto-completion with Git? 如何启用Git自动补全功能?

When using Git on different platforms, auto-completion is not always enabled/available. 在不同平台上使用Git时,自动补全并非总是启用/可用。

Note that this question is NOT a duplicate of git auto-complete for branches on the command line , because that question was only about Apple users. 请注意,此问题不是命令行上分支git auto-complete的重复,因为该问题仅与Apple用户有关。 This question is mostly the same, but including other platforms too. 这个问题大致相同,但也包括其他平台。

Apple users 苹果用户

See question git auto-complete in the command line 在命令行中查看问题git自动完成

Linux users Linux用户

Solution tested with Debian 9 (Stretch) 经Debian 9(Stretch)测试的解决方案

sudo apt install bash-completion

Pros: 优点:

  • Very easy for newcomers to Linux/scripts 对于Linux /脚本新手来说非常容易
  • No "manual" downloading 没有“手动”下载
  • Only makes use of the official repos: the "safest" way. 仅使用官方存储库:“最安全”的方式。
  • Easy to update: sudo apt update then sudo apt upgrade 易于更新: sudo apt update然后sudo apt upgrade

I also know that the question was asked for Apple users, but it shows up first in Google for "git auto-completion branch", so I thought a Linux answer would still be useful. 我也知道该问题是针对Apple用户的,但它首先在Google中显示为“ git auto-completion branch”,因此我认为Linux答案仍然有用。

You need to get bash 's programmable completion configured to return the possible commands. 您需要将bash的可编程补全配置为返回可能的命令。 You can see the "Programmable Completion" section in the bash man page if you want to implement it yourself. 如果要自己实现,可以在bash手册页中看到“可编程完成”部分。

A much easier method to use a script to pre-configure bash's programmable completion. 使用脚本来预配置bash的可编程完成的一种简单得多的方法。 On most distributions, the file for git completion, /etc/bash_completion.d/git (on Fedora and derivatives), /usr/share/bash-completion/completions/git (on Debian and derivatives) (other distributions might have it in other places, try locate or find to locate it) are provided by the git package. 在大多数分布,为混帐完成的文件, /etc/bash_completion.d/git (在Fedora和衍生品), /usr/share/bash-completion/completions/git (在Debian和衍生产品)(其他发行可能有它在其他地方,请尝试locatefind它)由git包提供。 (If it is not included with your git package, you can get it from here ). (如果它不包含在您的git包中,则可以从此处获取它)。

You can configure bash to source the file when it is started (probably using .bashrc ), or you can install bash-completion , which will automatically load completions like the one that the git package installs (and generally completions from /etc/bash_completion.d/ or /usr/local/etc/bash_completion.d/ (typically BSDs)). 您可以将bash配置为在启动文件时使用源文件(可能使用.bashrc ),也可以安装bash-completion ,它会自动加载git软件包安装的完成文件(通常是/etc/bash_completion.d/完成/etc/bash_completion.d//usr/local/etc/bash_completion.d/ (通常是BSD))。 On most distributions, bash-completion should be available in the repositories and you can simply install the package. 在大多数发行版中, bash-completion应该在存储库中可用,您可以简单地安装该软件包。 For other systems, see the instructions included in the README.md to install it. 对于其他系统,请参阅README.md包含的README.md进行安装。 (On non-GNU/Linux systems, some completions might need to be disabled to prevent it from outputting errors when starting) (在非GNU / Linux系统上,可能需要禁用某些完成功能以防止启动时输出错误)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM