The Wayback Machine - https://web.archive.org/web/20241001082454/https://www.geeksforgeeks.org/introduction-to-git-aliases/
Open In App

Introduction to Git Aliases

Last Updated : 08 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
git config

Introduction to Git Aliases

Method 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.

Config files

Introduction to Git Aliases

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

Introduction to Git Aliases

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’

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

  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)

How To Check All Aliases In Git?

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

How To Add An Alias For Git Status? 

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



Similar Reads

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 full shell syntax in git commands. How Do I Creat
5 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
What are type aliases and how to create it in Typescript ?
In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name. Aliasing a primitive isn't very practical as it's easy using primitive types
3 min read
React.js Blueprint Variables Color Aliases
In this article, we will learn about the Color Aliases offered by the blueprint.js library. BlueprintJS is a React-based UI toolkit for the web. It is written in Typescript. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications that run in a modern web browser. Variables Color Al
4 min read
Mongoose Schemas Aliases
Mongoose is a MongoDB object modeling and handling for a node.js environment. Mongoose Schemas Aliases help in converting a short property name in the database into a longer, more verbal, property name to enhance code readability. Creating node application And Installing Mongoose: Step 1: Create a node application using the following command: mkdir
2 min read
TypeScript Aliases Type
In Typescript, Type Aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you’d have to define by hand otherwise. Aliasing doesn’t truly create a new type; instead, it gives that type a new name Syntaxtype AliasName = ExistingType; Where the AliasName is the custom name y
3 min read
How to Setup Path Aliases in Vite React?
Path aliases are a powerful feature in modern JavaScript development that simplifies module imports by allowing you to use shorthand paths instead of relative paths. In Vite React projects, setting up path aliases can enhance code readability and maintainability by providing clear and concise import statements. Prerequisites:NodejsReactApproach to
3 min read
TypeScript Differences Between Type Aliases and Interfaces Type
TypeScript's type aliases and interfaces both define object shapes, but they differ in usage and capabilities. Interfaces are extendable and best for defining object contracts, while type aliases are more flexible, supporting union types and primitives, and offering broader versatility in type definitions. Type AliasesType Aliases in TypeScript all
5 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct purposes and are used in different contexts. In t
5 min read
Git - git prune
Git prune is used to remove the unreachable objects and clear up the local repository. Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. Table of Content What is git
6 min read
Git For Non-Developers: How Different Industries Are Adopting Git?
While Git is widely recognized for its role in software development, it's increasingly being adopted by non-developers across various industries. This shift is driven by the growing need for version control, collaboration, and efficient project management in fields like marketing, design, data science, education, and even legal and finance sectors.
8 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Git vs. Other Version Control Systems: Why Git Stands Out?
Version control systems (VCS) are an essential tool for developers, enabling them to track changes, collaborate on projects, and manage codebases efficiently. Among the various VCS options available, Git has emerged as the dominant choice, used by millions of developers and organizations worldwide. While other systems like Subversion (SVN), Mercuri
8 min read
Introduction to Git Branch
Branching means diverging from the mainline and continue to work separately without messing with the mainline. Nearly every VCS has some form of branch support. In Git, a branch is simply a reference to the commit, where the following commits will be attached. Git vs SVN: In recent years, the use of git has risen tremendously. Unlike SVN, git allow
3 min read
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. This article will introduce you to Git, explain it
6 min read
Git - Changing History
Git provides a very important option of altering the commit history. As the master of git, you can control and make it function according to you. Git gives you the authority to change the order of commits, change messages, remove commits entirely. Moreover, it also allows you to manage the project's history. However, it is advisable to commit the w
3 min read
Git Features
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it which may b
9 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: Non-bare Repositories Bare Repositories What is a N
7 min read
Getting changes from a Git Repository
Git allows performing various operations on the Repositories including the local repositories and remote repositories. The user when downloads a project to work upon, a local repository is created to store a copy of the original project. This local repository stores the changes that are being made by the user. This way, the user can edit the change
4 min read
Git | Working with Stash
Pre-requisites: Git Git allows multiple users to work on the same project simultaneously. Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developer's branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot comm
8 min read
Handling repositories with Git remote
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Syncing ability of the git allows the developers to access and work on any project anytime anywhere. The collaboration among developers via git takes place when git provides each individual deve
4 min read
Article Tags :