Can I disable a particular git command?
Asked Answered
F

2

6

With new versions of git new commands have been added which I will probably never use.
Is there a way I can disable these commands so that I my tab completion is faster?
For ex: before, git check<tab> would autocomplete to git checkout
But now git check<tab> doesn't tab complete due to there being git check-mailmap in the newer git version.

This is just one of the example.

Alternatively it would be great if I could "force" git to tab-complete "check" to checkout .

Edit: I use vanilla bash with no extra modifications

Fyrd answered 12/6, 2018 at 10:7 Comment(4)
Do you use vanilla bash ? Do you have zsh or any completion plugin ?Suitcase
What you actually need may be modifying the specified auto-complete file.Paddlefish
@Suitcase I use vanilla ubuntu bash. I will add it to the question for clarification.Fyrd
You should consider using the newer git switch <branch> instead of checkout for your navigation between branches. If you accidentally type git checkout <filename>, you undo all the changes to the file. It's git switch -c <branch> to create a new branch and switch to it.Maritsa
A
8

The official way is to use the configuration completion.commands and remove the ones you don't want:

git config --global completion.commands -check-mailmap

However, you can do even more. There is a hack in __git_main() used for testing that you can abuse to do what you want:

GIT_TESTING_PORCELAIN_COMMAND_LIST="$(git --list-cmds=list-mainporcelain,alias)"

This will force Git's completion to show only the main commands (and aliases).

You need Git v2.18 or newer for these to work.

Asseverate answered 25/6, 2019 at 15:4 Comment(3)
Wow, that's a heck of a lot cleaner than deleting stuff out of git-completion.bash. I had no idea this existed.Sirdar
Couldn't make it work on zsh… am I doomed? It's impossible to search about it, all results are generic auto-complete setup guides.Knepper
@Knepper Zsh's completion is hopeless. You can use my own completion for Zsh which uses Git's official completion though: github.com/felipec/git-completionAsseverate
S
3

To see how to remove items from the autocomplete, see FelipeC's answer.

An alternative is to use git aliases to create shorter alternatives to the commands you commonly use. For example:

git config --global alias.co checkout

Now you can type git co to check out files.

Sirdar answered 12/6, 2018 at 11:5 Comment(4)
I didn't know we could set aliases! This is much better than what I wanted originally. Thank you!Fyrd
The _git_foo() functions only get called after the user has typed 'git foo <tab>`.Asseverate
@Asseverate Feel free to post a better answer about how to remove commands from Git's autocomplete. That would help everyone.Sirdar
@RyanLundy I was typing exactly that.Asseverate

© 2022 - 2024 — McMap. All rights reserved.