简体   繁体   中英

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

How to enable auto-completion with Git?

When using Git on different platforms, auto-completion is not always enabled/available.

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. This question is mostly the same, but including other platforms too.

Apple users

See question git auto-complete in the command line

Linux users

Solution tested with Debian 9 (Stretch)

sudo apt install bash-completion

Pros:

  • Very easy for newcomers to Linux/scripts
  • No "manual" downloading
  • Only makes use of the official repos: the "safest" way.
  • Easy to update: sudo apt update then 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.

You need to get bash 's programmable completion configured to return the possible commands. You can see the "Programmable Completion" section in the bash man page if you want to implement it yourself.

A much easier method to use a script to pre-configure bash's programmable completion. 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. (If it is not included with your git package, you can get it from here ).

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)). On most distributions, bash-completion should be available in the repositories and you can simply install the package. For other systems, see the instructions included in the README.md to install it. (On non-GNU/Linux systems, some completions might need to be disabled to prevent it from outputting errors when starting)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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