Git Aliases


Today I would like to discuss the advantages of using git aliases, some considerations, and then I will share with you the aliases that I normally use:

What are Git aliases?

If you are familiar with git, you should at least know a few of the most popular commands. For example:

git checkout

Git provides a way to write an alias, or shortcut, to execute the same command. For example:

git co

Why using aliases?

One of the main reasons to use aliases is to type less. In this example, we just save typing 6 characters every time we need to run the command.

I tested this and it took me about 1 second to write the word checkout, but only a few milliseconds to type co.

The reason for this is that QWERTY keyboards (the most popular keyboard configuration) are designed to type as slowly as possible, so it's not surprising that typing a simple word could take so long.

Even if you can type faster than me, I’m sure that you can type co a lot quicker than checkout. Saving a second may not seem like much, but it has a cumulative effect on commands that are frequently used.

I tend to believe that it’s less likely to have spelling mistakes in a short command (although I have no proof of this), so if you use a short alias you’ll probably won’t need to re-type the command. And even if there’s a typo, re-writing a shorter command should be faster than typing again the whole thing!

One time, I met an engineer who used the aliases git co and git oc for the git checkout command. She explained to me that she’d frequently change the order of the letters when typing, so this was her way to ensure that whatever she entered in her terminal would get the desired outcome.

Considerations

Whenever you create a new git alias you will have to remember it (otherwise it defeats the purpose). For this reason, my advice is to create aliases as you need them, and not just grab a list of aliases that someone else has created.

When I joined a new job some time ago I met someone that knew how powerful git aliases are. Because he wanted to help me, he shared his git aliases with me. I thanked him for his good intentions, but in the end I did not use his list. Why? Because:

  1. that list was tailored to his own needs, and
  2. the characters that he used did not mean anything to me, so I really struggled at trying to remember them

To me, co means checkout, but perhaps you have an easier time remembering ck, or ct, or cho? We’re all different, and our brains work in different ways.

I truly believe that you should use git aliases, and that it should be something that makes sense and adds value to you.

How to setup a git alias

When setting up a git alias you can specify that they apply to your entire working environment (a.k.a. global), or just on a specific repository. It’s worth noting that whatever you decide is stored locally (this is not sent to the remote repository), and that you can update it anytime.

This example shows how to set a global alias:

git config --global alias.co checkout

And this is how it’s done on a specific repository.

cd <repository-directory>
git config alias.co checkout

You can find more details in the official git documentation: https://git-scm.com/book/ms/v2/Git-Basics-Git-Aliases

The git aliases I use

As I mentioned earlier, I strongly believe that everyone should come up with their own aliases. That said, I see no harm in giving some inspiration by sharing the aliases that I normally use:

git co # instead of git checkoutgit br # instead of git branchgit ci # instead of git commit
git st # instead of git status

I usually squash my commits before raising a PR. This is something I do so often that I created an alias for it:

git sq # instead of git rebase -i

Since most times I squash commits I just use the last two commits in my branch, I created an alias just for that:

git sq2 # instead of git rebase -i HEAD~2

I also tend to rename branches quite often (I may publish something on this in the future). Since I could never remember the command, I decided the create an alias that is easy for me to remember:

git rename # instead of git branch -m

Also, when I squash my commits I usually need to see a quick list of the commits in a branch. I created an alias for that:

git ll # instead of git log --oneline

When working with large repositories with hundreds or thousands of contributors, running git pull can take a lot of time, as the command pulls all new branches, not just the latest changes in the current branch. The command to just update the current branch is git pull <origin> <branch_name>. As you can guess, I created an alias for that too:

git po # instead of git pull origin

When not to create an alias

As you may have observed, I have intentionally only created a handful of aliases. This is because these are the commands that I use the most, so it’s easy for me to remember them.

I don’t create aliases for commands I don’t use often, as I would waste a lot of time trying to remember them, which defeats the purpose.

Destructive commands are another thing that I avoid for aliases, particularly git reset (and especially with the --hard option.) If I make a mistake and use the wrong alias I could accidentally lose hours of work, which is against the idea of using aliases in the first place. In these cases, I prefer to play it safe and spend one or two seconds rather than risking losing some of the work I’ve done.

Summary

Git aliases are a nice feature that used appropriately allows us to be more productive. As any tool, we need to learn to use it in our favour to make the most of it.

What git aliases do you use? Do you have a specific use case for an uncommon git alias? Please share it in the comments!

Jose Miguel


Leave a Reply

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