Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I do this:

alias g='git'

I loose all completion rules (i.e. branches and remotes are no longer being automatically completed when I hit TAB after typing, for example g push o).

share|improve this question
    
While I don't have an answer for you, I'm going to have to take a minute and wonder if not typing those two other letters is really giving you much of an advantage? – Marco Ceppi Sep 19 '11 at 13:17
3  
Sure it does! I'm typing 'g' probably over a 100 times a day. – Paweł Gościcki Sep 19 '11 at 13:29
3  
Aliasing often-used short commands to even shorter aliases saves me hundreds of keypresses a day. According to my shell history, I have used the alias g=git 756 times in the past month, meaning I saved pressing the 'g' and 'i' keys 1512 times total. That, combined with my git aliases, probably saves me tens of thousands of key presses a month. – Evan Teitelman Dec 12 '12 at 1:56
    
The main point of shortening keystrokes is helping your hands keep up with your brain. – Tyler Collier Feb 26 '15 at 23:37
    
The script at superuser.com/questions/436314/… worked so well I think it deserves a mention. – brandizzi May 26 at 14:50
up vote 14 down vote accepted

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
share|improve this answer
    
Ha! Wunderbar! Works just like it should :) – Paweł Gościcki Sep 19 '11 at 13:32
    
I also had to add source /usr/share/bash-completion/completions/git to my .bashrc – pcx Aug 21 '14 at 6:46
    
Not sure if the .bashrc code here works, but this answer is definitely out of date. /etc/bash_completion.d/git doesn't seem to exist on 14.04. See lzap's answer below. – Max Wallace Nov 6 '15 at 18:04

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 OSes (e.g. Fedora 22+) when you encounter:

completion: function `_git' not found

during completing.

share|improve this answer
2  
Seems like this is the only correct answer here. – pjvandehaar Nov 3 '15 at 6:17
    
Use __git_complete gc _git_checkout (or similar) to set up completions for more specific aliases. Search /usr/.../git as above to find the right name. – Max Wallace Nov 6 '15 at 18:00

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

share|improve this answer

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
share|improve this answer
1  
You skipped the part where they actually added completion in the comment you linked to: __git_complete g _git – pjvandehaar Nov 3 '15 at 6:08

Just for the sake of completeness, I'd like to add an answer using the ~/.bash-completion file, which gets sourced at the end of the bash-completion script:

_xfunc git __git_complete g __git_main
_xfunc git __git_complete gl _git_log
_xfunc git __git_complete gd _git_diff
_xfunc git __git_complete gb _git_branch

Then in my ~/.bashrc I have just the aliases. I was trying to:

  • avoid poluting my ~/.bashrc with bash-completion stuff (keep stuff where it belongs) ✓
  • avoid sourcing the whole git-completion into my shell ☹

Unforutnately the _xfunc sources the git-completion anyway. I'll update this answer once I figure out how to do it properly (I also asked on lunchpad here).

share|improve this answer

Look at here: https://gist.github.com/scue/576310b7c6b7714aad05

wget https://gist.github.com/scue/576310b7c6b7714aad05/raw/459d46761c231f5c373c1cf496920b01bb6669d2/.bash_aliases.git -O ~/.bash_aliases.git
echo "test -e ~/.bash_aliases.git && source ~/.bash_aliases.git" >> ~/.bashrc

Enjoy!(^o^)/

share|improve this answer
1  
Sorry, but that's something completely different. Making such simple alias es for whole commands is trivial. – Paweł Gościcki May 15 '15 at 8:42
    
I just make them work like git plugin of Oh-My-Zsh: github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/… – scue May 27 '15 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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