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!

Comments

May 20, 2011, 10:54

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!

May 20, 2011, 11:14

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.

Philip Permalink
May 20, 2011, 11:58

Save your bash aliases to .bashrc.alex and source it on login each time.

Snapp Permalink
May 20, 2011, 19:01

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.

May 20, 2011, 10:55

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/

May 20, 2011, 11:21

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

locobox Permalink
May 20, 2011, 18:16

I like your recommendation, thanks!

Git Pro Permalink
May 21, 2011, 11:07

First: aliases are only active in interactive shells
Second: if I need to use one of these programs I just type 'command gs'

October 22, 2011, 00:43

Or you can quote the command, which skips the alias. e.g. $ "gs"

dmitry Permalink
May 20, 2011, 11:29

I use 'git status' so often that I use 'g' instead of 'gs' :)

May 20, 2011, 15:29

@Dmitry

Then you need git-prompt ( http://volnitsky.com/project/git-prompt ), you will forget about your git status.

May 20, 2011, 11:53

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

Jason McCandless Permalink
May 20, 2011, 11:58

Surely these aren't huffman codes, since they don't have the prefix property.

May 20, 2011, 13:29

mine are

alias gl='git log --pretty=format:"%Cgreen%h %Creset %s %Cblueby %an (%ar) %Cred %d" --graph'
alias gd='git diff --stat --color'
alias ga="git add ."
alias gb="git branch \$1"
alias gu="git pull"
alias gp="git push"
alias gc="git commit -am \$1"
alias gs="git show \$1 --color"
alias gco="git checkout \$1"
Stephen Permalink
May 20, 2011, 13:29

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

May 20, 2011, 14:13

Thanks Peteris, alias is really cool!

Snapp Permalink
May 20, 2011, 18:48

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]

May 21, 2011, 06:21

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

October 22, 2011, 00:39

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

May 11, 2013, 19:37

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.

macy Permalink
October 17, 2013, 01:59

Amazing work! click here to see my work.

July 24, 2014, 19:29

I work with multiple hosted git accounts (on GitHub and BitBucket) for personal and professional projects, and I only operate using SSH. Thinks can get complex for my work, and I find what actually DOES save me hours (and reduces headaches) are a few succinct bash functions I wrote to enhance my git flow... I wrote a blog post detailing the rhyme and reason behind them, and I also take some time going through basic Bash programming concepts.

I welcome anyone to check it out, in case it can be useful! http://computethis.ghost.io/how-i-git-it/

Shiva Prasad Permalink
November 28, 2014, 05:32

I am new to git, learnt basics. Git doesn’t automatically infer your command if you type it in partially. If you don’t want to type the entire text of the Git commands,at that time you can easily set up an alias for each command using git config. that is the place where we use git config for alias

Example:

$ git config --global alias.co checkout
$ git config --global alias.br branch

Nice article about git aliases, thanks for sharing with us. And have a look at this document to find detailed information of SBI IFSC Code of Chittor.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "cloud_260": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements