Git

How to Set Aliases in Git Bash for Windows?

In Git, aliases are the shortcut for the Git command. It is a way to create a convenient name for a particular command or series of commands. This can be helpful for frequently utilized or complex Git commands. Moreover, it saves users time and makes it easier to use Git.

This write-up will explain the method to set aliases in Git Bash on Windows.

How to Set Aliases in Git Bash?

In Git, there are two possible ways to set aliases, such as:

How to Set Aliases Using Command Line?

To set aliases in Git Bash, the “git config –global alias.<alias-name> <git-command>” command is used. Check out the following examples for a better understanding.

Example 1: Set Alias for “git status” Command

Utilize the below-listed command to set an alias for the “git status” command:

git config --global alias.s status

Here, “s” is the shortcut for “status”:

Now, view the Git status using the “git s” command instead of “git status” to ensure whether the alias “s” works or not:

git s

The below output shows the Git status which indicates that the alias has been set successfully:

Example 2: Set Alias for “git add” Command

To set an alias for the “git add” command, run the provided command:

git config --global alias.a add

Here, “a” is the shortcut for “add”:

Run the “git a .” command instead of the “git add .” command to verify the alias:

git a .

The above-listed command has added the untracked files to the Git index:

Example 3: Set Alias for “git commit” Command

Write out the below-stated command and set the desired alias for “git commit” command:

git config --global alias.c commit

Here, “c” is the alias for “commit”:

Now, verify the alias by performing the commit operation using the following command:

git c -m "Files added"

It can be seen that the files have been committed successfully:

How to Set Aliases Manually?

To set the aliases manually, first, move to the “C:\Users\<user-name>” path on your PC. Then, look for the “.gitconfig” file and open it. After that, set the desired aliases in that file.

Step 1: Navigate to Desired Path

First, redirect to the following path on your PC:

C:\Users\<user-name>

Note: This is the general path and works for every Windows system:

Step 2: Open “.gitconfig” File

Now, look for the “.gitconfig” file and open it:

Step 3: Set Aliases in File

Finally, set the desired aliases of the Git commands in the “git config” file:

The above output shows the aliases that we have set.

Conclusion

There are two ways to set aliases in Git Bash, such as setting aliases using the command line or setting them manually. To set aliases using the command line, the “git config –global alias.<alias-name> <git-command>” command is used. To set the aliases manually, first, navigate to the “C:\Users\<user-name>” path on your PC and look for the “.gitconfig” file. Next, open the file and set the desired alias in it. This write-up explained the method to set aliases in Git Bash on Windows.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.