Git tip: aliases

The title is a little bit misleading, because this post is going to cover both a Git and a Bash tip. But this is my blog, so I title my posts the way I want.

Please, bring on some tips!

This is a tiny little trick that may be useful only if you ask Git to fulfil your wishes using the command line. Which you should probably be doing at this point, if you want to call yourself a real developer.

Git commands are totally long and totally hard to remember. True. But what if they could be shortened?

Well, in a way, git commands can be shortened. Or at least, adapted to your personal preferences, thanks to aliases.

Aliases can be global or specific to a repository. For example, if you are bored of typing:

$ git pull origin

You can create a “local” alias:

$ git config alias.po pull origin

And from that moment on, you can just do this:

$ git po

And voilà.

If you want to apply the alias to all your repos:

$ git config --global alias.po push origin

And voilà again.

My favorite? This one:

$ git config alias.last 'show -s HEAD^commit'
$ git last

Cool, but what about the bash tip?

Yep, I have one of those as well.

bash (the only true Unix shell) allows the creation of shortcuts to certain commands, called alias. For example, if you find yourself typing a thousand times a day the following command:

$ git checkout master

Wouldn’t it be neat to be able to just type the following?

$ go master

Well, that’s easy to do in just to steps. First, edit the bash configuration:

$ pico /.bashrc

Now add the following alias to that file:

alias go="git checkout"

Save and exit, and force bash to refresh:

$ . /.bashrc

(Note the space between the first dot and the curly-thingy-that-we-use-in-Spanish-on-top-of-the-letter-n-to-make-it-sound-funny)

Now, when you want to checkout a branch, just do:

$ go dev/my-development-branch

Ain’t that neat?

Leave a Reply

Your email address will not be published.