Pre-requisite: 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.
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
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.
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.
[alias]
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.
Similar Reads
Introduction to Git Aliases
Git is a tool for version control, but its commands can sometimes be long and difficult to type repeatedly. That's where Git aliases come in. Aliases allow you to create shortcuts for commonly used commands, making your workflow faster and more efficient. In this article, we'll introduce you to Git
5 min read
Git List All Branches
Branch management is one of the most powerful features of Git. Branches allow multiple developers to work on different parts of a project simultaneously. Effective branch management keeps your repository organized and reduces the chances of merge conflicts and workflow disruptions. Understanding how
5 min read
Basics Of Git
Git is the most popular distributed version control system which is commonly used by developers. It enables several users to work on a document concurrently and keep a record of the changes made and various versions of the documents in a project. Table of Content What is Git?History and Evolution of
13 min read
How to Add All Files in Git ?
Adding all files in Git is a common task when you want to stage all changes for committing. Adding all files in Git involves staging all modifications, additions, and deletions in your working directory for the next commit. This process ensures that all changes are included in the commit history. In
3 min read
Saving a File in Git
Git allows you to track changes, collaborate with others, and manage codebase efficiently. One of the fundamental tasks when working with Git is saving files, a process that involves several steps, including adding files to the staging area and committing them to the repository. In this article, we
2 min read
Using Git on CommandLine
Git is very important and powerful tool when it comes to the software development life cycle. Now in this article, we are going to see why git is used vastly in the software industry and what kinds of operations we can perform with git. So git is basically a distributed version control system for tr
5 min read
Git List Branches
Managing branches in Git is important in any development workflow. Whether you are working on different features, fixing bugs, or collaborating with a team, knowing how to list and navigate branches is important. In this article, weâll explore how to list branches in Git and understand different bra
3 min read
Git - Life Cycle
Git is used in our day-to-day work, we use git for keeping a track of our files, working in a collaboration with our team, to go back to our previous code versions if we face some error. Git helps us in many ways. Let us look at the Life Cycle that git has and understand more about its life cycle. L
3 min read
Git - Move Files
When managing a project using Git, you may find the need to reorganize your directory structure or rename files. Git provides powerful tools to handle these tasks seamlessly while keeping track of the changes. This article will guide you through the process of moving files within a Git repository, c
3 min read
Git - Index
In Git, the index, also known as the staging area, plays an important role in the version control process. It acts as an intermediary between your working directory and the repository, allowing you to prepare and review changes before committing them. This article explores the concept of the Git ind
3 min read