Open In App

Introduction to Git Aliases

Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: Git

Git aliases provide a way to shorten popular git commands. For example, you can use the “git co” command in place of “git commit“. In other words, it maps a custom command to a standard GitHub command. Like in the example above, the command “git co” was mapped to “git commit”. The user can use both commands, i.e. the original command and the alias to accomplish the same task.

How To Create An Alias In Git?

Method 1: Using The Git Config To Create Aliases

Here aliases co, br, ci, and st were created globally for the commands checkout, branch, commit, and status respectively.

If the tag “–global” isn’t used then the aliases will be local by default.

git config –global alias.<custom_command_name> <original_command Name> 

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
git config

 

Method 2: Directly Editing Git Config Files

Aliases can also be created by editing the .gitconfig files manually. A global alias is stored in the global .gitconfig file which is available under the user home directory in Linux, for a local alias it is inside the .git folder within the repository, or you can say “/.git/config” is the relative path for the file.

The contents of the config file are as shown below.  The file contains various information like user email, name, alias, etc. After creating aliases they will be listed under the alias header. One can insert aliases under the [alias] header manually.

Config files

 

Let us take an example to understand how git aliases work. Let’s say we have a repository Power-Puff-Girls, having 3 branches.

git branches

 

So here we’ve used the command “git br” which works the same as “git branch” command listing the branch present in the repository. Note that we can still use the “git branch”  Aliases can also be used to create a new command from a sequence of git commands.

Example:

Here we have combined reset and HEAD commands to create a new git command. Both the commands, “git unstaged app.css” and “git reset HEAD — app.css” are equivalent.

git config --global alias.unstage 'reset HEAD --'

Method 3: Using Aliases To Create New Git Commands

By using the git aliases command we can create custom commands that execute a series of git commands and can perform specific actions. You can add the command or any other URLs any thins by using the alias command as shown below:

Create a new branch with a customized name: 

You can use the alias name that you want to give to the command and the name of the command. 

alias  <name> ='<command name>’

Examples:

Git commits and push can be done in one command and you can create a shortcut instead of using the full command by using an alias command.

alias gcp=’git commit -m “Work in progress.” && git push origin HEAD’

Types of Git Aliases

A Git alias can be either local or global. A local alias is defined for a particular git repository whereas a global alias can be used in any repository. If you create an alias inside a git repository without the global flag, then the alias will be local by default i.e. it can be used in the current repository only. Alias created with global flags can be used in any git repository.

Benefits Git Aliases

  1. Git aliases can make you a faster and more efficient developer as they can save you a lot of keystrokes in the long run. For example, git commit is a frequently used command, using git ci every time instead of git commit is going to save a few keystrokes.
  2. It makes the command look simpler and clearer.

Frequently Asked Questions (FAQ’s)

1. How To Check All Aliases In Git?

Answer:

“git config –get-regexp alias” By using this command you can list all the aliases.

2. How To Add An Alias For Git Status?

Answer: 

“git config –global alias.<alias-name> status” With the help of the git config command you can ass git status with an alias. 


Last Updated : 02 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads