Pages

Git aliases with autocompletion

If you are using git on daily basis, you probably want to create some shorthands, aka. aliases, to save time. The very first option that is documented in tutorials and books about git is through git configuration.

$ vi ~/.gitconig

[alias]
st = status
co = checkout
br = branch
up = rebase
ci = commit

The problem is that this approach doesn't save you many strokes. You still have to type git st for git status. Well, you can add an alias for git like alias g=git. Now you can type g st instead. Still, I don't like to hit the spacebar for every command. So I create the following aliases for my most commonly used commands instead.

alias gst='git status'
alias gcm='git commit -m'
alias glog='git log --oneline'
alias gau='git add -u'
alias gco='git checkout'

You can go on with the list but I usually don't remember the aliases unless I use them very often so I tend to keep them short. Now the problem is that git autocompletion doesn't work with these aliases any more. Fortunately, this question has been asked and answer before. The solution can be found here. I take no credit for it. I just want to document it here for my future usage.
  1. Create a file named alias-completion.sh and save it somewhere that you won't accidentally delete it.
  2. Copy and paste the code in the solution here and save it in the file above.
  3. Go to ~/.bash_profile and add this: source /path/to/alias-completion.sh
  4. Reload profile: source ~/.bash_profile
Now you can use the aliases and git completion together.

No comments:

Post a Comment