5 Minute Guide to Git Aliases

Your busy. You’ve got stuff to do. Here’s Git aliases in 5 minutes.

What?

That’s right! A colleague recently showed me these and I didn’t know they even existed. Mostly, if I wanted to have an alias (certainly on Linux), I would use the alias command, but this might be useful if you don’t have that feature on your OS.

How to Create an Alias

You create aliases using git config -global alias. like this:

1
$ git config --global alias.co 'commit -m'

The part after the alias. is the name of the alias, and the rest of the line is what it will be replaced with. In this example, we’ve created an alias which let’s us do this:

1
$ git co 'A new commit message'

As you can see, the co is replaced with commit -m, making it equivalent to:

1
$ git commit -m 'A new commit message'

Running Commands

It’s also possible to use git aliases to run commands using the exclamation mark character. This is an example:

1
$ git config -global alias.rgi '!rm .gitignore'

Very contrived, I know, but this command (rgi = remove git ignore) will delete the .gitignore from the current directory.

Switching Accounts

This example is a little more useful. If you are sharing a server account (eek!?) and your colleague and yourself are both using it to commit changes to a repo, you will want to switch identities each time. You could do that with a normal alias, or as in this case, use git. Here’s how:

1
$ git config -global alias.me '!git config -global user.name "Stephen Moon" ; git config -global user.email stephen@example.com'

Now, I can switch accounts with this:

1
$ git me

Switch out the name an email address with your own, and you will be good to go.

One Last Thing

Are you wondering where these aliases end up? Take a look at the file .gitconfig in your home directory and you can see the entries under an [alias] section.


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com