How to Configure Git Aliases

How to Configure Git Aliases

ยท

2 min read

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.

Removing an alias

To remove an alias you can either delete the command from the $HOME/.gitconfig file directly or run:

git config --global --unset alias.<alias-in-file>

For example, if you wanted to remove the alias to check the git status, you could run the following command: git config --global --unset alias.st

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 are tons more options that you can easily configure. Thanks for reading and happy coding!

Resources