DEV Community

Cover image for A guide to Git Aliases
Sarah Lean 🏴󠁧󠁒
Sarah Lean 🏴󠁧󠁒

Posted on • Edited on • Originally published at techielass.com

A guide to Git Aliases

Typing long commands can get tiresome, this is where aliases can come in handy!

In this blog post I’ll share how you can set up aliases for your Git commands. Using aliases allows you to set up shortcuts that will make your life easier. There are two ways in which you can set up your aliases.

Set up aliases using the Git config command

You can set up your aliases using a command within your command line tool.

The format for the command is:

git config --global alias.<alias-name> '<git-command>'
Enter fullscreen mode Exit fullscreen mode

In practice that means if you want to type git c instead of typing git checkout the alias you would configure is:

git config --global alias.c checkout
Enter fullscreen mode Exit fullscreen mode

Setting up a Git alias

Set up aliases using the .gitconfig file

Another approach is to make modifications to the .gitconfig file. This is usually stored within your home directory, on my Windows machine it was stored under _C:\users\sarah\ _

Within this file create an alias section like below:

[alias]

# Shortcut for status
st = status

# Shortcut for checkout
co = checkout

# Shortcut for branch
b = branch

# Shortcut for help
h = help

# Shortcut for last commit
last = log -1 HEAD

# Shortcut to query log for altered files
l = log --stat

# Shortcut to query log and make it pretty
ld = log --graph --decorate --date=short
Enter fullscreen mode Exit fullscreen mode

Deleting a Git alias

If you want to remove an alias you can delete the configuration from the .gitconfig file or you can use the following command:

git config --global --unset alias.<alias-name>
Enter fullscreen mode Exit fullscreen mode

Deleting a Git alias

Check all aliases in Git

If you want to check which aliases are currently set within your configuration you can use the command:

git config --get-regexp '^alias\.'
Enter fullscreen mode Exit fullscreen mode

Using Git aliases

Now that the aliases are set up you can now use them when you are within your command line tool. I can now use git l in lieu of git log –stat.

πŸ’‘ It’s important to remember, these aliases are only applicable to your local machine.

Top comments (0)