Skip to content
Related Articles

Related Articles

Aliases in Git

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Apr, 2022
View Discussion
Improve Article
Save Article

Aliases are the user-friendly names given by a user to a git command so that the user doesn’t need to memorize the whole lengthy syntax of a git command. Let us go through some key terminologies:

!: Somewhere in the ~/.gitconfig file, you will see this symbol. This is used to allow full shell syntax in git commands.

Simple Aliases

There are two ways of creating aliases in git with the ~/.gitconfig file. So as we can see that the ~/.gitconfig file is opened and we can open it using any default text editor present in our git bash for example:- by typing vi ~/.gitconfig or by vim ~/.gitconfig. So in this file under the [alias] section of the file, we can create aliases by typing the name of the alias and assigning it to the git command for example:- Here we can see that an alias ci is created and we have assigned it to the commit command in git. In a similar way we can assign an alias name to the git command here we have assigned st as an alias name to the status command in git and for adding files in git we have assigned ankit as an alias to add files in git.

making aliases in git using ~/.gitconfig file

Here we are using the aliases that we have created in the ~/.gitconfig file and we can see that they are working perfectly as our git commands work. Here alias ankit which was created to add files in the repository is adding files accurately, st is also accurately showing the status of the git repository, and ci which was created to commit files is also working fine.

Using aliases for git commands that we defined in the ~/.gitconfig file

with the command line

This is the second strategy to create aliases in git using the command line where we can type the below command listed, the name of the git command you can put any whose alias you want to create. Here we can see that a –global parameter is used so that we can use the alias globally which means here an alias by the name of lol is recognized globally by the git bash and whenever we type git lol it gives us the output of git log command.

git config --global alias.alias_name "name_of_the_git_command

using lol as an alias for git  logs

Here we are creating shinchan as an alias for the git commit command and we can see that by using git shinchan we are able
to see the git commit command output. 

Using shinchan as an alias for the commit command 

List/search existing aliases

We can list existing aliases using –get-regexp which will list all the aliases that we have made using the ~/.gitconfig file and by using the command-line interface.

Using the command git config –get-regexp ‘^alias\.’ to see all the aliases

Searching aliases

To search aliases, we need to add the following line under the alias section of the ~/.gitconfig file. This line helps us in searching for aliases that we have made.

Added the line in the ~/.gitconfig file needed to search aliases in git

Then we can see we can use git aliases to see all the  aliases that we have created and if you type 
git aliases name_of_the_git_command will show you all the aliases that we have made for that git command.

Using git aliases to see all aliases that we have made
Using git aliases commit which shows aliases related to commits

Temporarily ignored tracked files

Now if we want to temporarily ignore a file from the tracking of git we can do so by using the aliases. By assigning an alias name to the git command update-index –assume-unchanged which is used for temporarily ignoring a file. So, we can open the ~/.gitconfig file using vi or vim editor whichever you want and in that, under the alias section, we can assign an alias name to the git command update-index –assume-unchanged.

Creating alias unwatch for the command update-index –assume-unchanged

To list all files that are being temporarily ignored for that we can edit the ~/.gitconfig file and in that, we can assign an alias name to the “!git ls-files -v | grep ‘^[[:lower:]]'”

Here we can see that we have assigned the alias unwatched the command to list all the ignored files present in the repository

Here we can see that on using the git unwatched command we can see all the ignored files present in the repository.

Using git unwatched to see all the ignored files present in the repository

To start tracking the file again we can assign the command update-index –no-assume-unchanged to an alias in the ~/.gitconfig file. Here we have used an alias by the name of the watch to be used for tracking the file again which was being ignored.

Assigning watch the git command to start tracking the file

Here we have used the alias watch and we can see that by using the git watch we can track the file1.txt which was being ignored earlier by the git.

Using the git watch alias for tracking the file again

Unstage staged Files

So now if you want to unstage changes present in a file for that we use the git reset command. Now if you want that some user-friendly name can be given to the command so that we don’t need to remember the syntax of the command for that we can use aliases.

Creating an alias for the reset command in git

Here we have used the git unstage to reset changes from the file1.txt and we can see that the changes have been unstaged and on doing git st which is showing the status of the git repository here is showing file1.txt in untracked files which means changes will not be tracked from now onwards if any in file1.txt.

Using alias unstage as the command 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!