How to Configure Git Aliases

Subscribe to my newsletter and never miss my upcoming articles

A Note on the Series:

Fast Fridays šŸŽ is a series where you will find fast, short and sweet tips/hacks that you may or may not be aware of. I will try to provide these (fairly) regularly on Fridays.

Aliases

An alias is simply a shortcut that can reference a longer command. When an alias is configured, git will substitute the alias with the command that it is mapped to. Setting up aliases for common git commands can speed up your workflow and efficiency as you will spend less time typing or trying to remember full commands.

Configuration

Aliases can be set up in two different ways:

  1. Using the git config command.
  2. Directly editing the git config files in your $HOME/.gitconfig file.

I would recommend going with option #1. I find this to be the fastest way to set things up so that will be the method I discuss. However, feel free to do whatever works best for you.

Let's say you wanted to set up aliases for three common git commands: branch, checkout, and status. To configure those aliases you would run the following in your terminal:

git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.st status

If you view the .gitconfig file in your home directory you should now see this:

  [alias]
      br = branch
      co = checkout  
      st = status

If you want to add a git alias for commands that have spaces, wrap the command in double ("") or single ('') quotes.

git config --global alias.undo "reset --hard"

The next time you want to run any of those commands, you can now utilize the aliases you just set up! For example, git br will now execute the same operation as git branch and git undo will now execute git reset --hard.

El Fin šŸ‘‹šŸ½

With a few simple short steps, you can quickly improve your git workflow. The examples I provided are some of the aliases I use but there tons more options that you can easily configure. Thanks for reading and happy coding!

Resources

Comments (3)

Skay's photo

It's a simple and useful post. Thanks for Sharing Alyssa Holland. Does this work on Windows as well?

Alyssa Holland's photo

Hey Skay! Iā€™m personally a Mac owner but if you are able to run git commands already on Windows, I would assume that you can configure aliases. I can try and do some research on that though.

Skay's photo

Alyssa Holland no worries. Let me try at my end and update my findings šŸ‘