Git aliases

Taking a slight sidebar from my current blog series entitled Getting going with Heroku and PHP (part 1) (and part 2), I’ve discussed that the commands that I am now using to push updates are as follows…

gulp
git add .
git commit -m "A useful commit message"
git push
git subtree push --prefix build heroku master

This is obviously fairly cumbersome to type on a regular basis, so I’ve turned to the wonderful world of Git aliases.

In short, Git allows you to create your own commands, so you can rename existing commands to something you prefer, a shorthand maybe, or even combine multiple commands into a new one.

In order to do this, you type a command such as…

git config --global alias.s status -s

This creates a config item globally that means that if I type the command “git s” it will actually run “git status -s”.  As you can see, only the parameters are defined in the alias, not “git” itself.

I love this, especially as I love using single character variable names, even though it totally annoys everyone.  They make sense to me.  Well know I can vent using time-saving Git aliases.

As I said before, you can go one step further and chain multiple commands together.  This is done by first indicating you want an external command by starting with an exclamation mark (!) and then using double-ampersand (&&) to join the commands together.  In this case you do need to include “git”.

For example, taking the first two git commands above, I can combine them like this…

git config --global alias.c "!git add -A && git commit -m"

Now when I want to add and commit together I can simple use…

git c "A useful commit message"

In fact, with the aliases I’m currently using, the above 113 characters spread over 5 commands has now become 44 characters spread over 3 commands…

gulp
git c "A useful commit message"
git p

That’s 39% of the characters used, quite a saving which I’m use will add up significantly over time.

Here is a full list of my current aliases…

alias.deploy=subtree push --prefix build heroku master
alias.s=status -s
alias.c=!git add -A && git commit -m
alias.p=!git push && git deploy

As you can see, you can even call an alias in an alias (“deploy”, in my case), which makes life even easier if you want to be able to call the command individually (and there’s no way I’m going to remember the “deploy” one without the alias!) or as part of a group.

What aliases do you use?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.