Why you should use git aliases (and how)

Why you should use git aliases (and how)

This article is about aliases for git.

What are git aliases?

Aliases are tools to improve your workflow and make your experience easier and more natural. They are not really complex and if you work with them regularly you can see that you are faster.

why should I use aliases?

The answer is really simple: speed improvement. It should be easy to understand that it is faster to write for example git p instead of git push. Aliases don't end at git you can use them in your terminal too, I recommend an alias for git so that you don't have to write every time the complete command.


How to create aliases

  1. You can add aliases to git in two ways. The first one is with a command. I don't recommend this way since it needs a lot of time in the beginning and I like to see what I edit. If you want to use this command edit this template:
    git config --global alias.<alias> <normal_command>
    
    example:
    git config --global alias.co checkout
    
  2. The other way is with the configuration file. I say again that I recommend this method. Here you have your configuration file open in a text editor. The config file is in your home directory in Linux you can open it in VS code with
    code ~/.gitconfig
    
    I covered the install process for VS code in this article: When you are using Windows or macOS the config file should be below your user. I think in this folder (windows) C:\Users\<your_username>. If this file doesn't exist you can create it and make sure you have git already installed.

There you have to add and modify this template

[alias]
    <alias> = <normal_command>

there you add one alias per line. My example looks like this

[alias]
    c = commit -m
    co = checkout
    p = push
    s = status

The benefit is that I only have to write git add <file> to add files, git c "<commit message>" to commit the file(s) with a custom commit message and git p to push the commit(s).

What I recommend

I suggest also an alias for git itself. with Linux or at least ubuntu, you can add alias g=git in your ~/.bashrc. If you are using another distribution or OS you should easy google how to do it and/or write a comment for other users with the same operating system.