Alias in English means that the original identity of someone (or something) is not as what they (or that) are known. For example, the rapper artist's alias name is Eminem, but his real name is Marshall. More technically, if we talk about python, a module such as matplotlib.pyplot can be imported as it is. But, using this big-name everywhere is not convenient. So ideally, we create an alias by typing the command import matplotlib.pyplot as py to indicate that wherever we use py, it means matplotlib.pyplot. Here, py becomes an alias. More or less similarly, we have "Git Alias", which is compresses big commands (ideally) or any command if that matters. This tutorial is a small share on the same topic which will follow up the index as:
- What is Git Alias?
- How to Create Aliases In Git?
What is Git Alias?
Alias is a term associated with shortcuts in Git. It compresses the longer sequence of commands to a shorter sequence. It is always better to apply aliases to the frequently used longer commands since it helps in increasing efficiency. By longer commands, I mean longer patterns of commands, which include different options and flags, but it is not necessary for the command to be longer per se. Moreover, a user can create an alias for any command they want and thinks if that command will be used very frequently by them. It is noteworthy that there is no such command as "alias" that exists in Git. In other words, alias is just the term used to create a shorter command out of a longer sequence of commands. So, since there is no concept as an alias, where do we do all this? Where is all this stored?
Aliases happen in the Git Config file. To know more about the git config file and how it is viewed and edited in Git Bash, you can visit the Git-Config tutorial. So once you create an alias, you can either save it as a local copy for your repo or globally. The user doesn't need to use alias name only; the user is free to use the original command also. There are no restrictions. Let's see how to create an alias along with a few flags whose usage is quite frequent.
How To Create Aliases In Git?
- Open your Git Bash in the working directory. Type the following code and execute the command:
git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
The above command can divide into three sections.
- git config --global = Config --Global tells the git to include the alias in the config file, or we are trying to edit the config file. The global keyword stores the alias globally.
- alias.hist = alias is the keyword telling the git that the word following it will act as the alias. "hist" is the word we will be using here.
- "log --pretty=format: '%h %ad | %s%d [%an]' --graph --date=short" = This is the actual command of which we will be creating an alias of. So, in the above screenshot, we are trying to create a shorter command for "log --pretty=format: '%h %ad | %s%d [%an]' --graph --date=short".
In the next steps, we will try out our newly created alias name to use in Git bash.
- Type git hist now to see if the alias works:
- Execute the command by pressing enter.
This way, we can create an alias for command and use them anytime. You can practice creating your own now.
Common Questions
Where are Git Alias stored?
They are in the config file of the Git. The user can choose to save the alias as a local or global variable. A local alias will be repository-specific only.
Is there any standard Git Alias that's most common?
No, there is no such alias because there is no such term as an alias in Git. Many platforms use hist as the common alias for git log command for viewing graphs and beautifying it a little bit. However, there are no such standard commands.