How to alias 'git' to 'g' so that bash-completion rules are preserved?

Solution 1:

Latest bash-completion upstream moved and renamed things a bit. It's now:

source /usr/share/bash-completion/completions/git
__git_complete g __git_main

Use this in recent versions of Ubuntu (e.g. 14.04, also Fedora 22+) when you encounter:

completion: function `_git' not found

during completing.

Solution 2:

Copying and modifying opportunely from /etc/bash_completion.d/git, add the following lines to your ~/.bashrc:

complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
    || complete -o default -o nospace -F _git g

Solution 3:

In ~/.bashrc:

alias g='git'
source /usr/share/bash-completion/completions/git
complete -o default -o nospace -F _git g

Via http://29a.ch/2013/8/9/fixing-bash-autocomplete-on-ubuntu-13-04

Solution 4:

First, look up the original completion command. Example:

$ complete | grep git

complete -o bashdefault -o default -o nospace -F __git_wrap__git_main git

Now add these to your startup script (e.g. ~/.bashrc):

# copy the original statement, but replace the last command (git) with your alias (g)
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main g

# load dynamically loaded completion functions (may not be required)
_completion_loader git

The _completion_loader line may not be required. But for some situations, the completion function is only loaded dynamically after you type the command and press TAB the first time. So if you haven't used the original command, and try the alias + TAB, you may get an error like "bash: completion: function not found".

Solution 5:

The updated way to do this (complete wouldn't work for me):

  1. cd - switch to your home directory
  2. wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
  3. Add source ~/git-completion.bash to your .bashrc file (if you don't have this file make one in your home folder, bash will look for it automatically)
  4. Add alias g='git'to your .bashrc file.
  5. Start a new session or source your changes with source ~/.bashrc