| by Arround The Web | No comments

Efficient Git Workflow with Aliases: Customizing Commands

Have you ever been tired of typing long commands over and over? I believe yes. No worries, introducing aliases in Git permits the users to shorten the commands and use the defined aliases to get the same output. This process comes in handy when users have to deal with lengthy commands.

This guide aims to uncover all the possible methods to create aliases for the Git Bash command with the following content:

How to Set Git Aliases For Customizing Command?

Three possible methods are considered to set the aliases for Git commands which we stated below.

Method 1: Customise Git Commands Through Aliases (Command Line)

The first method to create the Git aliases is through the command line. For that purpose, you can use the following syntax to create aliases for Git commands

Syntax

git config --global alias.<alias name> <command>

Just put the alias name in the above syntax, enter the Git command, and run it in the terminal. For a practical demonstration of this method, check out our guide on how to Create Git Aliases.

Method 2: Customise Git Commands Through Aliases (Config File)

The second method to create Git command aliases is through the config file. This file is located in the hidden directory “.git” folder which is automatically created when the directory is initialized. Let’s check and implement this method in the following steps.

Step 1: Move to Project Directory

Open the terminal and move to the project directory using the “cd” command:

cd aliases

We have navigated to the “aliases” repository.

Step 2: Access the “.git” Folder

Next, again use the “cd” command and move to the “.git” folder:

cd .git

Step 3: List Content

To see the “config” file, list the content of this hidden directory:

ls

As you can see the “config” is present in the directory.

Step 4: Configure Config File and Define Aliases

Open the “config” file in the nano editor and define the aliases in it:

nano config

To set the aliases, type the “alias” in square brackets, and write the alias name, “=” and Git command as we have added below:

[alias]

st=status

co=commit

a=add

l=log

chk=checkout

After defining the aliases, save the file by pressing “Ctrl+O” and exit from the editor using “Ctrl+X

Step 5: Verification

Let’s verify that our alias has been set. For instance, we will use the “git st” instead of “git status” command:

git st

As you can see the “git st” command is working fine.

Likewise, you can check other defined aliases as we are using the “git co” command to commit the changes:

git co -m "initial commit"

Method 3: Create Git Commands Through Aliases

The user can also create an alias for the whole command. To do so, check out the following syntax.

Syntax

alias <alias name>="Git Command"

Use the “alias” command, define the alias name put “=”, and write the Git command to set the alias.

To see how it works, quickly look at the 2-step guide.

Step 1: Create Aliases Command

Let’s say we want to set the alias “log” for the “git log” command. To do this, the following command is considered:

alias log='git log --oneline'

Alias “log” has been set.

Step 2: Use Alias

Verify that the alias is set, run the “log” in the terminal to see the log history:

log

The log history is listed.

Conclusion

To set the aliases for customizing Git commands, three methods are considered. First, through the command having the syntax “git config –global alias.<alias name> <command>”. Second, use the “config” file available in the “.git” directory and define the aliases in it. Third, set the alias directly for the whole git command using the syntax “alias <alias name>=”Git Command”.

Share Button

Source: linuxhint.com

Leave a Reply