Aliases in Git
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.
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.
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.
– 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
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.
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.
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.
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.
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.
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 on using the git unwatched command we can 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.
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.
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.
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.