How to configure a Git alias

Arif Billah Babu

         

  git



Image not found!!

Configuring Git aliases can help you create shortcuts for commonly used commands, making your Git workflow more efficient. You can set up aliases using the git config command or by editing the .gitconfig file manually. Here's how you can do it:

Method 1: Using git config command

To create a Git alias using the git config command, use the following syntax:

bash
git config --global alias.alias_name 'original_command'

Replace alias_name with the desired alias and original_command with the Git command or sequence of commands you want to alias.

Example:

bash
git config --global alias.co checkout

Now, you can use git co instead of git checkout.

Method 2: Editing .gitconfig manually

  1. Open the .gitconfig file in your home directory (or create it if it doesn't exist).
bash
# On Unix/Linux/Mac nano ~/.gitconfig # On Windows notepad %USERPROFILE%\.gitconfig
  1. Add the alias manually under the [alias] section. If the section doesn't exist, create it.
ini
[alias] alias_name = original_command

Replace alias_name with the desired alias and original_command with the Git command or sequence of commands you want to alias.

Example:

ini
[alias] co = checkout

Save the file after making changes.

Using Aliases

After configuring an alias, you can use it like any other Git command. For example:

bash
git alias_name [arguments]

Example:

bash
git co feature-branch

This will be equivalent to git checkout feature-branch.

Listing Existing Aliases

To list all configured aliases, you can use:

bash
git config --get-regexp alias

This will display a list of all aliases along with their corresponding commands.

Remember that these aliases are stored globally if you use the --global option with git config, making them available across all your Git repositories. If you want to set an alias for a specific repository only, omit the --global option and run the command within the repository.