Your busy. You’ve got stuff to do. Here’s Git aliases in 5 minutes.
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.
You create aliases using
git config -global alias. like this:
$ 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:
$ git co 'A new commit message'
As you can see, the
co is replaced with
commit -m, making it equivalent to:
$ git commit -m 'A new commit message'
It’s also possible to use git aliases to run commands using the exclamation mark character. This is an example:
$ 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.
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:
$ git config -global alias.me '!git config -global user.name "Stephen Moon" ; git config -global user.email firstname.lastname@example.org'
Now, I can switch accounts with this:
$ git me
Switch out the name an email address with your own, and you will be good to go.
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
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