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
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.
Let us take an example to understand how git aliases work. Let’s say we have a repository Power-Puff-Girls, having 3 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’
Introduction to Git Aliases
Git is a powerful version control system widely used in software development for tracking changes, coordinating work among team members, and managing codebases. Git aliases provide a way to simplify and customize your workflow by creating shortcuts for commonly used commands. This article will introduce you to Git aliases, explaining how to create, use, and manage them to enhance your productivity.
Contact Us