Git alias bash completion

October 2018 ยท 1 minute read

How to setup bash completion for a git alias under Debian. I have a few git aliases setup. One which is dedicated to handle my dots. Bash completion did not work out of the box on my machine, this does of course make some commands annoying and a waste of time. Especially when you’re working with long paths. I was able to get replicate $ git <tab><tab> by adding av few lines to my .bash_aliases. 1

# Config file handling
alias dot="/usr/bin/git --git-dir=$HOME/.dots/ --work-tree=$HOME $@"

# Git completion for "dot" alias
source /usr/share/bash-completion/completions/git
__git_complete dot _git

More specific commands, e.g. alias gc="git checkout", require a minor tweak on the last line.

alias gc="git checkout"
source /usr/share/bash-completion/completions/git
__git_complete gc _git_checkout

The checkout part can be replaced with most, if not all, git sub-commands.

fred@bridge:~$ _git
_git               _git_checkout      _git_diff          _git_help          _git_merge_base    _git_reflog        _git_shortlog      _git_tag
_git_add           _git_cherry        _git_difftool      _git_init          _git_mergetool     _git_remote        _git_show          _git_whatchanged
_git_am            _git_cherry_pick   _git_fetch         _gitk              _git_mv            _git_replace       _git_show_branch   _git_worktree
_git_apply         _git_clean         _git_format_patch  _git_log           _git_name_rev      _git_rerere        _git_stage
_git_archive       _git_clone         _git_fsck          _git_ls_files      _git_notes         _git_reset         _git_stash
_git_bisect        _git_commit        _git_gc            _git_ls_remote     _git_pull          _git_revert        _git_status
_git_branch        _git_config        _git_gitk          _git_ls_tree       _git_push          _git_rm            _git_submodule
_git_bundle        _git_describe      _git_grep          _git_merge         _git_rebase        _git_send_email    _git_svn