Last Updated: July 06, 2017
·
6.065K
· diasjorge

Creating custom git commands + autocompletion

Git has a really nice feature to allow you to create your own commands that behave like they we're part of git.

Any script that follows this naming convention git-COMMAND_NAME that is accessible on your PATH can then be accessed in the shell like:

git COMMAND_NAME

Adding autocompletion to this custom command in Bash is very straight forward. Git's autocompletion script will look for a function with the command name like this:

_git_COMMAND_NAME ()
{
  # you can return anything here for the autocompletion for example all the branches
  __gitcomp_nl "$(__git_refs)"
}

For further details you can take a look here: https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

1 Response
Add your response

This is not a very thorough example at all... I'm no pro at reading git source and I don't aim to be, but I would like to add autocompletion to some of my custom git commands. I dropped the code above into one of my scripts, changed COMMAND_NAME to the name of my git command, and... nothing. It's not clear what this example even should do or how to use it in the context of an existing custom command without investing hours learning the source of git.

So thanks. Thanks for popping up on google search with your lazy, high-level example. In other words, thanks for nothing.

over 1 year ago ·