Git is one of the most advanced version control systems to support easy branching and merging, multiple staging areas, and a distributed workflow paradigm. To get more out of Git, you can use one of its properties called “Git Alias.” Like a person’s nickname, this alias feature saves time by merging repetitive steps for a faster user experience in the Git terminal window. Here we show you what Git aliases are, how to add them, and some of the most useful examples.
Git Alias: What Is It?
Git Alias may be defined as a created command that automatically infers the text of a larger command to bring out the repetitive references as a shortcut process. The programmer can use Git aliases to design commands that should exist or makes logical sense towards efficient committing.
Here’s one of the examples of a global alias being created for “checkout,” which is used to interchange branches in a repository. Instead of the long command shown here, one can now simply use git co
.
git config --global alias.co checkout
A Git alias for branch is being used to create a new branch in Git repository. Instead of the long command, one can use the alias git br
.
git config --global alias.br branch
Finally, here is an alias defined for Git commit, with a shortcut key: git ci
.
git config --global alias.ci commit
To get started with Git Alias, you must first install Git correctly. After you’ve done that, proceed as shown here.
How to Add Git Aliases
Adding Git aliases in the terminal window makes Git Bash pretty straightforward and easy. But before you do that, you need to position your commands in the right Git repository, otherwise it will lead to a “fatal: not a git repository” error.
To position your commands and to ensure you’re working in the right directory, you have to initialize the repository as shown here. It’s only required to do this step once in the beginning. In the following example, the Repo has been reinitialized.
git init
Enter clear
to wipe the screen clean. Now you can start experimenting with Git aliases among other functions.
Let’s add a Git alias for a “Status” command. This is useful to keep track of any changed files in the workflow. You can define the alias for this as “st” as shown here.
git config --global alias.st "status"
Once done, as shown here, you can simply type git st
as a shortcut. There’s nothing added to commit to here because all these file systems are untracked, but the status alias does work properly.
More Git Alias Examples
1. Git Commit
Doing any work at your end in Git would require committing to the Master repository. This makes Git commit one of the most essential alias commands.
git config --global alias.ci commit
2. Git Revert Commit
Sometimes you need to undo a commit made using the Git Bash terminal alone. The Git alias for this revert can be defined as follows.
git config --global alias.rc revert<commit#>
3. Git Remove Commit
For a hard deletion of the commit, which removes it entirely from the branch, you need to use the reset command alias for specific versions.
git config --global alias.rs reset
4. Git Change Commit Message
Want to change the messaging of the most recent commit? The Git Change Commit Message plays a role in this. The following can be an alias for it. Of course, it will impact the most recent message.
git config --global alias.am amend
5. Git Change Branch
Do you want to swing from one Git branch to another? That’s where the checkout alias assumes importance.
git config --global alias.co checkout git co <existing branch> git co -b <new branch>
In a similar manner, we can create appropriate Git aliases for some of the following commands:
- Check Git username: something like
git config --global user.name "James Joyce"
can be aliased to avoid frequent mentions of the username. - Git show branches: want to see all the branches together? You need to find an alias for
git config --global branch;
for remote branches, add- r
to the command.
The above tutorial explains the systematic, step-by-step methodology of creating Git aliases for the most frequently used commands in the Git Bash terminal window. Here’s how you can get started with GitHub project management. You can also host your blog for free with Jekyll and GitHub Pages.
Our latest tutorials delivered straight to your inbox