Follow me on Twitter for my latest adventures!
I was just watching a friend of mine work with git, and he'd always type all the git commands in full, like git status and git push. I realized that he must not be the only one to do so, so I decided to write this quick blog post and encourage everyone to create Huffman coded aliases for the most commonly used commands. Instead of typing git status, alias it to gs. Instead of git add, alias it to ga, etc.
Here are a bunch of aliases that I created for 99% of git commands I ever use:
alias ga='git add' alias gp='git push' alias gl='git log' alias gs='git status' alias gd='git diff' alias gdc='git diff --cached' alias gm='git commit -m' alias gma='git commit -am' alias gb='git branch' alias gc='git checkout' alias gra='git remote add' alias grr='git remote rm' alias gpu='git pull' alias gcl='git clone'
Here is my typical workflow with these command:
$ vim file.c $ gd # git diff $ ga file.c # git add file.c $ gm "added feature x" # git commit -m "added feature x" $ ... $ gp # git push
Short and sweet!


Facebook
Plurk
more
GitHub
LinkedIn
FriendFeed
Google Plus
Amazon wish list
Comments
Seems like you should probably have "vim" aliased to "v" if you're into Huffman encoding your commonly used commands? :-)
I have a love/hate relationship with aliases, I think it's difficult to find the balance between efficiency and screwing yourself over when you find yourself on an unfamiliar machine. For example I have "git st" at work, but not on my laptop at home... is the time I save at work really saving me time if whenever I'm coding at home I have to type:
~$ git st
[ receive error message ]
[ grumble about aliases ]
~$ git status
?
Of course git config and .vimrc you can always set up some way of keeping all your machines in sync, because those are things you'll usually only be using on your local machines. The real difficulty comes with bash aliases, where you might spend 50% of your time on a remote server you share with other people who might not want you adding your aliases to the shared .bashrc you all use.
Anyway that's my rambly rant about the dangers of aliases. Let me know if you've found some way to solve them!
i've got into the habit of putting my confs into a git repo themselves with a script to setup symlinks to the "proper" file in my home folder.
then on a unfamiliar machine i just clone and run the script or if it's already checked out i just pull in changes. saves me the majority of the headaches.
Save your bash aliases to .bashrc.alex and source it on login each time.
I work with a few hundred systems where I have the same profile files on all systems for my user. My primary workstation has the git repo defining the files:
* profile
* bashrc
* aliases
* aliases-workstation
* functions
* functions-workstation
The files with the "workstation" suffix are only sourced when the bashrc file detects that I'm on my workstation.
As for managing the same profile across a bunch of systems, just use rsync to push it out to everyone and create the appropriate symlinks like jaymz mentioned.
I do understand that this solution wouldn't necessarily be something you'd want to implement for root since as you pointed out that is likely shared across multiple users.
If you want git to behave like other VCS, you can add an [alias] section to your .gitconfig:
[alias]
co = checkout
ci = commit
st = status
You can also add aliases to your .gitconfig that create whole new git functions:
[alias]
some-thing = !git bla bla --option 2>/dev/null
See this rival blog post for that concept :) http://blogs.gnome.org/danni/2011/03/07/useful-git-aliases/
Aliases but - real git-aliases - are one of the things I really often use, create new ones - but mostly based on what I do often - so check your console-history and create aliases based on your own needs:
Creating Git Aliases based on your own needs.
One of the cons of your short system-aliases:
You can accidently overwrite a system-program - there are many short ones which you, your system or other tools/scripts may use:
gc - count graph components
gm - GraphicsMagick command-line utilities to create, edit, or convert images
gs - Ghostscript (PostScript and PDF language interpreter and previewer)
I am already using a really long list of aliases because a big part of my workflow is based on git and its submodules:
Git Aliases for Submodules
I like your recommendation, thanks!
First: aliases are only active in interactive shells
Second: if I need to use one of these programs I just type 'command gs'
Or you can quote the command, which skips the alias. e.g. $ "gs"
I use 'git status' so often that I use 'g' instead of 'gs' :)
@Dmitry
Then you need git-prompt ( http://volnitsky.com/project/git-prompt ), you will forget about your git status.
Perer,
Did you use Huffman coding to create the aliases you have shared abobe? I am not able to think how did you decide upon the decoded value, even if you know the frequency. I am aware of the conventional example of Huffman coding which uses binary codes to exemplify the codes of alphabets based on their frequency of occurrence. If you have done Huffman coding, it would be lovely to see the code.
~Harsha
Surely these aren't huffman codes, since they don't have the prefix property.
mine are
I've done the same sort of thing, but with git-sh (so that aliases don't apply when you're not in a git-sh session) and also adding in grep for dealing with deep directory hierarchies (Java projects). E.g.
ag foo
Will git ls-files all un-added files, grep for foo, and add those. foo can be a directory/file name/extension etc. Same thing for dg ("diff grep"), rsg ("reset grep"), etc.
It's slightly out of date, but I posted about my setup here:
http://www.draconianoverlord.com/2010/03/04/git-config.html
Thanks Peteris, alias is really cool!
Like others I typically use git aliases along with some bash functions to script out very commonly used series of commands. But there is value in pairing down all the way using shell aliases as well. In that case I'd recommend registering your aliases with bash completion should you have that installed (ie. bash-completion package on debian).
Just add something like this to your bashrc file:
# apply default git completion to custom aliases
complete -o bashdefault -o default -o nospace -F _git_add ga
complete -o bashdefault -o default -o nospace -F _git_branch gb
complete -o bashdefault -o default -o nospace -F _git_checkout gc
complete -o bashdefault -o default -o nospace -F _git_clone gcl
complete -o bashdefault -o default -o nospace -F _git_diff gd
complete -o bashdefault -o default -o nospace -F _git_log gl
complete -o bashdefault -o default -o nospace -F _git_commit gm gma
complete -o bashdefault -o default -o nospace -F _git_push gp
complete -o bashdefault -o default -o nospace -F _git_pull gpu
complete -o bashdefault -o default -o nospace -F _git_remote gra grr
That ought to give you nice completion based on the command
example:
# equivalent to `git branch master`
gb ma[tab]
Hey Peteris,
Great post! And one I'd thought about before but never really put into practise.
However, there is one small problem with your solution. What if your alias conflicts with a unix command that already exists? e.g. GhostScript is 'gs'.
I've come up with an idea that I call "context sensitive aliases". The idea is the if you're in a git repository then 'gs' means 'git status' otherwise it means 'gs'.
I whipped up a little script that creates context sensitive aliases.
-------
#!/bin/bash
if [ $# -lt 2 ]; then
exit 1
fi;
CMD=$1
shift
echo "$CMD=(git status > /dev/null 2>&1); if [ \$? -eq 0 ]; then $@; else $CMD; fi"
-------
Then you can define your aliases as follows in a .bashrc file or similar.
alias "$(git-alias ga git add)"
alias "$(git-alias gds git diff --staged)"
alias "$(git-alias ga git add)"
alias "$(git-alias gp git push)"
alias "$(git-alias gl=git log)"
alias "$(git-alias gs git status)"
alias "$(git-alias gd git diff)"
alias "$(git-alias gm git commit -m)"
alias "$(git-alias gma git commit -am)"
alias "$(git-alias gb git branch)"
alias "$(git-alias gc git checkout)"
alias "$(git-alias gra git remote add)"
alias "$(git-alias grr git remote rm)"
alias "$(git-alias gpu git pull)"
alias "$(git-alias gcl git clone)"
Sean
Hi,
I also use aliases, but I decided to speed them up even more by giving numbered shortcuts to all the files from git status. It really does make it blazingly fast to stage and commit files. You can take a look at my project here, if you like: http://madebynathan.com/2011/10/18/git-shortcuts-like-youve-never-seen-before/
Thanks,
Nathan
Thanks for posting. Trying to get a new machine up and running and it's been a while since I touched my aliases.
Your post was helpful. One thing I like to do is use aliases for common misspelled words like "comit"
alias comit="commit"
in case I'm typing things out for some reason.
Comments here encouraged me to come up with a better way to backup my profile.
Leave a new comment