In this post, we will learn to create an alias in git. If we don’t want to type the whole command then we can create an alias for them. Aliases make it easy and simpler to work with git commands. To use aliases we need to create them first, we can create aliases using the git config command.
General command to set alias is as follows:
git config --global alias.keyword command
I’ll create alias for “git status” command. Command to create alias is as follows:
git config --global alias.st status
As you can see “git st” command return the same result as “git status” because of alias we created.
Now we will create a alias to unstage the files
Here I made some changes in the example.txt and added it to the staging area now we can unstage this file using the “git restore –staged example.txt” command but I want to set an alias for it. So for that, I can use the following command:
git config --global alias.unstage 'restore --staged --'
As you can see after the unstage keyword we have defined our command in single quotes. At the end of the command, there are two hyphens(–) which act like an input parameter and represent filename to unstage. After executing the aliased command we can that our file is now unstaged.
Now we will set alias to see last commit using following command:
git config --global alias.last 'log -p -1'
Above instead of using “git log -p -1” we are using git last. Last, is the alias assigned for “log -p -1” which returns the last commit. So this is how we can use aliases in git since using alias work become easier.