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.
How Do I Create Git 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.
Directly Editing Git Config Files
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.
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.
Using The Git Config To Create Aliases
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
6 Useful Git Aliases
Below are the most used aliases
1. Git Config List
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.
2. Git Search Commit
You can search the commits along with the git log command. By using various options and filters with it as shown below.
git log –grep=”Mention the commit you want to search”
To search the commits that were made by a specific author you can use the below command.
git log –author=”Name of the author”
3. Temporarily Ignored Tracked Files
You can ignore the tracked files temporarily by using the below command.
git update-index –skip-worktree
the flag –skip-worktree allows you to mark the files as skipped by this git will not track the files anymore.
4. Unstage Staged Files
So now if you want to unstaged 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 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.
5. Git status
The git status command is often used to see the files which are not tracked by git. By using an alias you can get what is the particular message you want from the git status.
git config –global alias.st ‘status -sb’
“st” will make the command short “status -sb” to get the required message needed.
6. Git log –oneline
By using the git –oneline command you can you can get the commit history in short form or in a single line.
git log –oneline
“–oneline” and “git log” provides a single line output that can be easy to read and understand the commit history.
Git Alias Multiple Commands
You can use the git alias to combine multiple git commands into a single shortcut. As shown below.
last-commit = !sh -c 'git log -1 && echo && git diff --stat HEAD'
If you execute the last-commit it will include the following.
“git log -1” will give the details of the last commit that you have committed. Echo is used for the separation of lines, and git diff –stat HEAD displays the changes made in the last commit.“!sh -c” allows you to execute the sequence of git commands to be executed.
Autocorrect Your Aliases
By using help.autocorrect configuration option you can enable auto-correct for git commands. If you type any mistake in the command git will automatically suggest the correct one.
git config --global help.autocorrect 1
If you mistype the git command to know the autocorrect is enabled git suggests the closet command to it.
Frequently Asked Questions
1. What is a git alias?
With the help of the git alias, we can create shortcuts to frequently used git commands.
2. How do I add a git URL to an alias?
You can add a URL to an alias by the git remote ass [alias name] [URL OF repository]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!