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 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.
git config --global alias.c commit
- After this the line below will be added to the /.gitconfig file. Make sure it was saved.
git config --list
- Then the alias will be visible.
c = commit
- Now the alias is accessible. It will work just as you typed the whole command.
git c -m "example"
- In the end, open the config file and you will see something like this.
[alias] c = commit
Creating git aliases by directly editing .gitconfig file
The second way of creating git aliases is directly editing git config files, like this:
[alias] co = checkout
Aliases for Git Commands
Here are some useful git aliases that just replace the original git command and are designed to make you type less:
|cob = checkout -b||Checkouts a new not yet existing branch.|
|f = fetch -p||Fetches from a repository and prune any remote-tracking no longer existed references on the remote.|
|p = push||Push the 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||Force to delete a branch.|
|dc = diff --cached||Displays the staged changes.|