A cool thing that I really liked on mercurial and that I miss in git is the possibility of using shortcuts. Mercurial has them hard-wired. For instance, instead of typing hg status
you could also type hg st
or if you want to commit something: hg ci -m "Commit Message"
, which comes far handier than using hg commit
and then waiting for the editor to start up and save the commit message file. There is also su for summary, and co for checkout. And this really saves you a lot of typing time, as these are the common tasks you want to use.
Git doesn’t come with that possibility, and while I wasn’t using it more often than Mercurial I didn’t bother to figure out how I could make git use some shortcuts. That’s of course possible, as Git is highly configurable. Now, I looked it up and I found out that there’s a highly sophisticated way of doing so.
Just open up your ~/.gitconfig
. In it you will already find something like this. You’ve set this up for your first git usage – if you haven’t used git you’ll need to do it, otherwise git won’t work:
[user] name = Your Name email = youraddy@yourprovider.tld
What we’ll add is a new section, starting with [alias]
Now for each alias we want to set up, we write “alias = command”. So, this is basically exactly the way you would do your custom aliases in Mercurial if you ever did any. If you haven’t, for instance let’s get a shortcut for status:
[alias] st = status [user] name = Your Name email = youraddy@yourprovider.tld
We can even add flags, if we want. So if status is too chatty for you, try using the short flag:
[alias] st = status -s
Now when calling git st
you get something like:
D README.md A cli_ruby.bibtex M notes.md ?? code/01_have_a_purpose/todo/
with A being a file added, D a file deleted, M for a modified file and ?? for untracked files. If you at any time prefer the chatty version for just this time, telling you how many commits your local HEAD is away from origin, and how to reverse changes, just use git status
again.
Of course, the power comes, when using highly sophisticated commands that you cannot possibly type every time you want to use them. For instance, here’s a shortcut that not only sets the graph, abbrev-commit flags, but also sets date to relative and formats the message in a short and neat way and presents them in custom colors:
[alias] st = status -s lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %Cblue<%an>%Creset' --abbrev-commit --date=relative
The above I found at Shawn Biddles git config. He’s really a great guy, you should visit his site and YouTube channel, as well as his dotfiles on GitHub – there’s a lot to learn. One great additional feature of aliases is that you can even execute shell scripts or use other aliases. One example:
You work with a team on a project and you only want to display your own commits in the log. One can achieve this by hardcoding the name into the alias:
my = log --author='Your Name'
That’s handy, but if you want to share your config with friends, they will have to alter the config. And even you might have different users and want to keep it flexible. So instead you could use the git function to get certain lines of your config, and execute it inside your alias
my = log --author="$(git config user.name)"
So that’s already neat, but let’s go further: Say you have different aliases that all need to use the current username, for instance you might want to get all the commits done by you today. Now you know a good programmer is a lazy programmer, so rather than retype “config user.name” so many times, let’s use an alias for it:
me = config user.name
And now we can do:
me = config user.name my = log --author="`git me`" today = log --since=midnight --author="`git me`" yesterday = log --since=day.before.yesterday.midnight --until=midnight --author=`git me`
Pretty neat, huh?