git alias

Description
A git alias, also known as a shortcut, creates short commands mapping the longer ones. It demands fewer keystrokes to run a command, which simplifies the developers’ work. Direct git alias command doesn’t exist. This type of command is created through the git config command and the git configuration files. Aliases can be generated in a local or a global scope with other configuration values.
Creating Aliases with Git
There are two ways of creating Git aliases: using the git config command and directly editing the .gitconfig file.
Creating Git aliases with git config command
In order to create Git aliases with the git config command, follow the steps below:
- To create a git alias you have to edit your .gitconfig file in the user directory, in order to make these aliases accessible for all the projects.
- Run the git config command and define the alias.
creating alias in git
git config --global alias.c commit3. After this the line below will be added to the ~/.gitconfig file. Make sure it was saved.
git config --list
git config --list4. Then the alias will be visible.
git alias
alias.c=commit5. Now the alias is accessible. It will work just as you typed the whole command.
git alias example
git c -m "example"6. In the end, open the config file and you will see something like this.
git alias in .gitconfig
[alias]
c = commit7. ### Creating git aliases by directly editing .gitconfig file
The second way of creating git aliases is directly editing git config files, like this:
git alias in .gitconfig file
[alias]
co = checkoutAliases for Git Commands
Here are some useful git aliases that just replace the original git command and are designed to make you type less:
| Aliases | Description |
|---|---|
| cob = checkout -b | Creates and checks out a new branch. |
| f = fetch -p | Fetches from a repository and prunes any remote-tracking references that no longer exist on the remote. |
| p = push | Pushes changes to a remote. |
| bd = branch -d | Deletes a branch only if it has been merged. |
| ba = branch -a | Lists both remote-tracking and local branches. |
| bD = branch -D | Forces deletion of a branch. |
| dc = diff --cached | Displays the staged changes. |
Practice
What is the correct method to create a Git alias for the 'commit' command using the 'git config' command?