The Complete Guide to Git Alias: Shorcuts to Be Efficient

Programmers understand the value of using git for version control and how it plays an important part in the development of software on a daily basis. However, there are git commands used frequently. It is not a secret for programmers to constantly look for ways to optimize processes, make things easier, and also become more productive. Those who want to become more efficient at git workflow should look into learning how to use git alias, which we will explain in this article.

Git alias is an alternative tool git has to generate shortcuts or custom commands for frequently used commands. For instance, as you work on different features it is common to checkout different branches using the git checkout command. By setting an alias to the checkout command, we could shortcut this command something like git co or git cout.

Learning how to use git aliases means remembering how to use more git commands, which would mean something else to remember to the already extensive list of things programmers have to keep on their brain. Luckily, using git aliases is rather simple and straightforward, which we will explain in this article and show you where the aliases are located, how to update and delete an alias, how to see all the git aliases, and why you should start using git aliases more often.

How to Create a Git Alias

You can set up a new git alias using the git config command followed by the keyword alias. Here is the syntax to generate an alias:

git config --global alias.<alias> <commands>

Where <alias> is the name of the alias or shortcut you want to assign, and <commands> is the command to execute whenever the alias is called. For instance, if we want to create an alias s for the status command, it would look like this:

git config --global alias.s status

Once the alias is created, we can call the alias just like if we were calling any command in git.

git s

There are git commands where you would typically pass an additional value such as the checkout command. If we need to checkout from “other_branch” to “master” branch, you would typically add the name of the branch after the checkout keyword.

git checkout master

Therefore, If you create an alias cout for the checkout command, like in the following example:

git config --global alias.cout checkout

You could still provide additional options when calling the alias cout like if you were executing the checkout command.

git cout master

In case you want to create an alias that automatically checks out the master branch, make sure to the branch when creating the alias.

git config --global alias.cout-master "checkout master"

Notice how we are using quotes whenever there are multiple values for the alias being created.

Where is my Git Alias Stored?

Notice in the previous section we used the keyword --global to generate the git alias. That means, the alias we generated is stored in the global configuration file. While it is common to create the alias in the global configuration, that doesn’t mean you have to generate it there all the time.

There are different configuration files you can use:

  • Global config file using --global keyword
  • System config file using --sytem keyword
  • Repository config file using --local keyword
  • Per Worktree config file using --worktree keyword
  • Any given config file using -f or --file <file> keyword

Hence, we can create the git aliases and store them on different configuration files. For instance, if we want to store the alias st for the status command in the repository configuration file, you would use the following command:

git config --local alias.s status

How to List Git Aliases

There are different ways to display the list of git aliases. One approach is to use the --get-regexp command to get the value of all configurations containing the word “alias” using regex.

git config --get-regexp alias

Another way is to use the --list option which will display the list of all the configurations.

git config --list

The only disadvantage of using --list is to display configurations that are not aliases such as branch names or the user’s personal information.

For a more targeted list of aliases, such as listing only global aliases, make sure to provide the keyword of the config file location. For the global config file, we use the --global option.

git config --global --get-regexp alias
git config --global --list

How to Update a Git Alias

To update a git alias, use the -e or --edit config option.

git config --global --edit

This will open the configuration file in the terminal in VIM mode. Using the keyboard, find the [alias] section and locate the alias you want to update. Update the command assigned to the alias. Once it is updated, press the ESC key. and type :wq and press the ENTER key. This will save the changes made in the config file as well as exit VIM mode in the terminal.

How to Delete Git Aliases

To delete git aliases, use the --unset option followed by alias.<alias_name>. For instance, if we want to remove the alias s as a shortcut of the status command, run the following command:

git config --global --unset alias.s

We had also created an s alias in the local configuration. We can run a similar command to remove it, but with a different config file option.

git config --local --unset alias.s

Why You Should Start Using Git Aliases

Thinking about git aliases as adding more complexity to the list of git keywords a programmer needs to remember can be a reason for programmers to stay away from adding shortcuts. However, the reality is the fewer time programmers spent on typing to implement a solution, the more efficient they are at delivering solutions.

Also, there are commands that are not easy to remember, which setting an alias can make it more readable whenever it is needed. For instance, if you need to revert to the last commit, you can execute the following command:

git revert HEAD

While HEAD references the last commit of the current branch, it might not be as readable as if you had a command name like revert-last. In that case, we can create an alias to revert to the last commit.

git config --global alias.revert-last "revert HEAD"

Now, we can execute an alias command which is user-friendly and easy to understand.

git revert-last

Conclusion

Using git aliases is a powerful tool software developers can take advantage of to develop shortcut commands to quickly execute git workflows and spend less time typing, becoming more efficient. It is also possible to create git aliases to provide a more explicit meaning of what a process does, making it easy to understand by only reading the git alias.

Interested in Learning more about Git?

I wrote other articles explaining how to use other git commands, and I thought you might be interested in reading some of them since you are reading this.

Did you like this article?

Share your thoughts by replying on Twitter of Become A Better Programmer or to my personal Twitter account.