Different ways to create Git Aliases (Shortcut Commands)

Git Aliases are the shortcut commands for regular git commands.

For example, a shortcut for git status can be gst and git branch can be gbr.

They are very useful when you are working a lot with git commands.

Here are different ways on setting and using Git Aliases:

1. Local Repository Alias (for current repository)

1) Open Terminal

2) Go to the folder of the repository where you want to set the git alias.


cd path/to/your/local/repo

3.1) Run the following command on terminal:


git config alias.st status
git config alias.br branch

This will add the following lines in your .git/config file:


[alias]
        st = status
        br = branch

3.2) You can also directly edit the .git/config file and add your alias there instead of running commands through the terminal.

Note that the above procedure is for a particular single repository.

If you want to set the aliases globally, you need to look at the procedure shown below.

2. Global Alias (for all repositories)

1) Open terminal.

2.1) Run the following command on terminal:

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

This will add the following lines in your ~/.gitconfig file:

[alias]
        st = status
        br = branch

2.2) You can also directly edit the ~/.gitconfig file and add your alias there.

3. Set the global alias on your bashrc or zshrc file

Open ~/.bashrc or ~/.zshrc

Add the following lines to the file:

alias g='git'

# git config commands can be directly executed on terminal
# instead of putting here
# this will eventually add alias to ~/.gitconfig file
git config --global alias.co checkout
git config --global alias.st status
git config --global alias.br branch

alias gst="git status"
alias gbr="git branch"

After you make any change to ~/.bashrc or ~/.zshrc, you need to reload the file:


source ~/.bashrc OR source ~/.zshrc

For Zsh shell users, if you are using oh-my-zsh configuration framework then you can also run the following command:


omz reload

Now, you can do the following to check Git status:

# Regular command
git status

# Shortcut command
g st

# Shortcut command
gst

Similarly, to list out Git branches:

# Regular command
git branch 

# Shortcut command
g be

# Shortcut command
gbr

4. Using git module for oh-my-zsh

If you are using Zsh shell and OhMyZsh configuration framework, then you can use the git plugin for oh-my-zsh.

It contains a lot of aliases already set and ready to use.

You can check all the git aliases in the below link of the git plugin page itself over here.

5. Using the Git Alias Project

This github repo contains a lot of git aliases. The project repo README contains a simple and easy step-by-step guide on downloading, installing and using the project file for git aliases.

Hope this helps. Thanks.