3

How can I configure git to recognise the shortest unique prefix of a command, like git does with SHA hashes, or like mercurial does?

e.g. git ad should be recognised as git add, or git conf as git config.

Clearly I could do this by adding a bunch of aliases, but that's far too brittle in the face of new commands, and tedious too.

Is there some built in functionality for doing this, or a plugin?

I've already got satisfactory text editor integration, and I'm not interested in aliases.

4

Well, if you don't want to add aliases, then the best approach that comes to my mind is just pressing TAB after the command. In example:

  • git con + TAB will result in git config.
  • git com + TAB will result in git commit.
  • git ad + TAB will result in git add.
  • etc.

PS: no, there is not such build-in functionality.

3
+50

It seems like you are asking for a new feature. Git is open sourced, so you can enhance it anyway you want:

https://github.com/git/git

You can pull the source code. I see that the commands are defined under an array named commands[] inside git.c. And matching appears to be done inside the following function:

static struct cmd_struct *get_builtin(const char *s)
{
    int i;
    for (i = 0; i < ARRAY_SIZE(commands); i++) {
            struct cmd_struct *p = commands + i;
            if (!strcmp(s, p->cmd))
                    return p;
    }
    return NULL;
}

You can modify the line of strcmp() to do your minimum prefix match. I think it should not take more than 20 lines of code.

  • Is there an existing feature request to comment? I would hope it would be more general, supporting sub-commands like git stash l for list. It could be enabled by an option. – Joshua Goldberg Nov 12 '18 at 17:43
0

Here's a fine article on creating aliases for git commands.

http://githowto.com/aliases

Also, your editor of choice may have built in git integration. Here's an example with sublime. I don't believe there are any plugins to modify git's built in command structure.

https://scotch.io/tutorials/using-git-inside-of-sublime-text-to-improve-workflow

-1

"in the face of new commands" — which new commands? In my opinion, that doesn't happen very often, and even then, it's likely not a command you're going to use often.

I'd recommend using Zsh with the oh-my-zsh Git plugin. It offers lots of useful aliases.

Of course, you could also just copypaste the aliases and use them in Bash, but you'd lose the nice auto-completion features. Also you'd have to make sure to define the helper functions as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.