Why do I need alias?

One of my favourite bits of “feedback” from Mercurial users is that “you don’t need to type out the full command with hg - git is dumb” or something like that. While they’re not totally incorrect here, they are comparing the out of the box experiences between git and hg.

Mercurial will recognise shorthand for commands when you type enough characters to distingush the right command, like:

hg com -> hg commit
hg sta -> hg status
hg sum -> hg summary
hg ci -> hg commit

That’s right, hg does aliases too…

Git however will scream at you when you mistype a command. And you will swear in return (either at yourself or at git).

So to save yourself from screaming in the future, you can customise git to recognise custom commands. It will save you so much time.

Aliases are stored per-user in the .gitconfig file - or in the .git/config file for a repository. All the below scripts will install in the global config file - but you can drop the --global parameter if you don’t want that behaviour.

Adding shorthand as aliases

Here’s some shorthand commands for the common git tasks:

# git st
git config --global alias.st "status"   
# git co
git config --global alias.co "checkout"
# git ci
git config --global alias.ci "commit"   
# git br
git config --global alias.br "branch"   
# git df
git config --global alias.df "diff"     
# git lp
git config --global alias.lp "log -p"   

These are some of the common examples from the here.

Customising existing commands

My favourite example of this is a prettier output from git log - so you can see at a glance the activity on the branch you are currently working on:

# git lg
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative"

Of course, someone far smarter than me wrote this one

A few simpler ones:

# git com -> checkout master
git config --global alias.com "checkout master"

# git fe -> fetch latest changes
git config --global alias.fe "fetch origin"

# git hr -> hard reset state of master
git config --global alias.hr "reset origin/master --hard"

# git me -> get my user name
git config --global alias.me "config user.name"

And you can even embed scripts into an alias. Here’s a couple of simple scripts to see what commits have been added for today (by me any by others):

# git today
git config --global alias.today "!git all --since='12am' --committer=\"`git me`\""

# git today-all
git config --global alias.today-all "!git all --since='12am'"

# git bn -> get current branch name
git config --global alias.bn "!git for-each-ref --format='%(refname:short)' `git symbolic-ref HEAD`"

Do you see where we’re going with this? :)

Mashing commands together

Remember how in my last post I mentioned a rather verbose workflow for merging in changes?

Well let’s take it away with one simple command: sync

# git sync
git config --global alias.sync "!git update && git co master && git pull && git co @{-1}"

But that’s rather basic - it doesn’t actually rebase my branch that I’m on.

Let’s do a slightly different command: resync

# git resync -> sync repository and rebase current branch    
git config --global alias.resync "!sh -c 'BRANCH_NAME=$(git bn) && git com && git fe && git hr && git rebase master $BRANCH_NAME'"

Note how this one uses aliases defined earlier in the post for readability? Yeah, it’s rad.

I’m lazy, give me the gist of it (lol pun)

I’m checking some other git repos I have lying around to ensure I have all my aliases in one spot, but in the meantime open your ~/.gitconfig file (check C:\Users\{username}\ if you’re on Windows) and paste these values under the [alias] node. If the [alias] node doesn’t exist, create it.

Got a cool alias you want to share? Fork the gist above!

blog comments powered by Disqus