Introduction to Git Aliases
Last Updated : 08 Jul, 2024
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 aliases, show you how to set them up, and provide examples of some useful aliases.
What are Git Aliases?
A Git alias is a shorthand for a Git command or series of commands. By creating an alias, you can replace a lengthy command with a short and memorable abbreviation. This not only saves time but also reduces the risk of errors when typing commands.
For example, instead of typing git status
every time you want to check the status of your repository, you could create an alias like gs
to achieve the same result.
Why Use Git Aliases?
- Efficiency: Save time by reducing the amount of typing needed.
- Consistency: Ensure you use the same commands consistently across different projects.
- Customization: Tailor your Git experience to suit your specific workflow.
- Ease of Use: Simplify complex commands into straightforward ones.
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.
Creating Git Aliases
Creating Git aliases is straightforward. You can set them up in your .gitconfig
file or by using Git commands directly in the terminal.
Method 1: Using Git Config
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
Introduction to Git AliasesMethod 2: Editing Git Config Files Directly
You can manually edit the .gitconfig
files to create aliases. Global aliases are stored in the global .gitconfig
file (located in the user’s home directory on Linux), while local aliases reside in the .git/config
file within the repository.
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.
Introduction to Git AliasesLet us take an example to understand how git aliases work. Let's say we have a repository Power-Puff-Girls, having 3 branches.
Introduction to Git AliasesSo 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'
Examples of Useful Git Aliases
Here are some examples of common Git aliases that can enhance your productivity:
1. Status Alias
Instead of typing git status
, you can use:
git config --global alias.st 'status'
Now, simply type git st
to check your repository status.
2. Log Alias
Viewing the commit history with git log
can be made easier with an alias:
git config --global alias.lg 'log --oneline --graph --all'
This alias (git lg
) shows a concise, graphical representation of the commit history.
3. Checkout Alias
Switching branches frequently? Simplify git checkout
with:
git config --global alias.co 'checkout'
Now, use git co [branch-name]
to switch branches quickly.
4. Commit Alias
For making commits with a short message, use:
git config --global alias.ci 'commit -m'
This allows you to commit changes with git ci "your message"
.
5. Add Alias
Staging files for commit can be shortened from git add
to:
git config --global alias.a 'add'
Use git a [file-name]
to add files to the staging area.
6. Push Alias
Pushing changes to the remote repository can be abbreviated from git push
to:
git config --global alias.psh 'push'
Use git psh
to push your changes.
Benefits Git Aliases
- 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.
- It makes the command look simpler and clearer.
Similar Reads
Git Introduction Git is a powerful and widely used version control system that helps developers track changes in their code, collaborate with others, and manage project history effectively. Whether you are a professional developer or just starting out, understanding Git is important for modern software development.
5 min read
Aliases in Git 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
5 min read
Top 70+ Git Interview Questions and Answers - 2025 Git is a distributed version control system (DVCS) designed to track changes in source code during software development. In 2005, Linus Torvalds, who is also the creator of the Linux kernel, developed it. Today, more than 40 million people use Git universally. If you are a developer, DevOps engineer
15+ min read
How to List Remote Branches in Git? Git is a powerful version control system that allows developers to collaborate on projects, maintain code history, and manage multiple lines of development through branching. One common task when working with Git is to list all branches in a remote repository. This article will guide you through the
3 min read
Bare Repositories in Git Repositories in Git are a snapshot of the folder in which you are working on your project. You can track the progress and changes made to the project by making commits and also revert changes if not satisfactory. Repositories can be divided into two types based on the usage on a server. These are: N
7 min read