9

I have implemented a custom git command by writing a shell script located in /usr/local/bin. It works fine, but I would like the script to autocomplete branches in the command line, just like git checkout [TAB][TAB]. How could this be done?

EDIT: Just adding some context: git allows you to very easily add your own commands by creating a script git-$subcommandName. In my case here git-install, the script facilitates checking out a branch, building, packaging, and installing the source code.

7
  • 2
    Not really reinventing the wheel here, my custom command "git install" takes a branch name, which it checks out (so yes, same as git checkout), but it then does a series of steps to package and install the source onto a compute cluster. Commented Dec 23, 2016 at 20:43
  • 1
    @pedrorijo91 aliases only allow you to reuse a preexisting git commands, which would not work for me. I don't think my question was clear so I just added an edit. Commented Dec 24, 2016 at 15:33
  • 4
    I nominated this question for reopening - it's a perfectly succinct question about a very specific need, with broad applicability. Commented Sep 28, 2017 at 21:06
  • 1
    git alias can be used for other things, you can have an alias such as !sh do_foo. Commented Oct 11, 2018 at 13:47
  • 4
    @KenWilliams, glad you did. I think those who voted to close should re-examine the question. :-( Even the unedited question isn't asking to re-implement all of git's bash autocompletion. (That would be insane!) It's very clear that it's limited in scope to a user-added custom command. The original poster even gave a very short answer that works perfectly, and gives a jumping off point for others implementing their own custom commands. Commented Nov 6, 2018 at 9:27

1 Answer 1

12

Figured it out. I needed to download git bash completion (here), create a new file in /etc/bash_completion.d with the following contents:

source ~/.git-completion.bash
_git_install ()
{
  __gitcomp_nl "$(__git_refs)"
}

and then exec bash to reload completion scripts.

Sign up to request clarification or add additional context in comments.

5 Comments

Note if your custom git command has a dash in it (i.e. log-diff) then the bash function you create must replace the dash with an underscore (i.e. _git_log_diff().)
I'll also add that to simply duplicate the exact completion as another command, the body of the bash function can just call another. Not what you want here, but if you did, the body of the bash function could just be _git_checkout "$@". Useful if you're making a custom command like log-diff and want it to complete identically to how log does.
There's no need to pass arguments, just _git_install() { _git_checkout ; }.
The answer doesn't mention what the name of the file in /etc/bash_completion.d/ needs to be. From some experimentation it looks like it doesn't matter, and you can put multiple functions inside the same file, so you can just name it git-commands and have the one source line at the top and then a bunch of bash functions for each of your git functions.
However, in some systems (e.g., Ubuntu 20.04), there is already an up-to-date file at e.g. /usr/share/bash-completion/completions/git. So, you can source that, instead of downloading one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.