An alias, or a shortcut, allows to replace a long or less memorable command with a simple one. In this post, I will talk about Git aliases.
If you use Git on the terminal or command-line, Git doesn’t automatically infer your command if you type it in partially. If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using
git config. Here is an examples you may want to set up:
$ git config --global alias.st status
Now instead of typing
git status, you just need to type
git st to check the status of your Git repository. Here are a couple of more examples you may want to set up:
$ git config --global alias.a add $ git config --global alias.b branch $ git config --global alias.c commit $ git config --global alias.co checkout $ git config --global alias.cob checkout -b
Another way of adding git aliases is to add it in
~/.gitconfig file. Just open the file with your favorite text editor and aliases like:
[alias] st = status a = add b = branch c = commit co = checkout cob = checkout -b
Creating a Git alias can also be very useful in creating commands that you think should exist. For example, while unstaging a file, you can add your own unstage alias to Git:
$ git config --global alias.unstage 'reset HEAD --'
This makes the following two commands equivalent:
$ git unstage fileA $ git reset HEAD -- fileA
Obviously, it looks more clean and clearer to use the
git unstage command than the
git reset HEAD --.
Git alias is not limited for the Git provided commands only. You can also run any external command by adding
! character at the beginning of it. This is useful when you write your own commands that work with a Git repository. For example, we can make an alias
git ui to run
$ git config --global alias.ui '!gitk' $ git config --global alias.ui '!sourcetree'
So far, I have talked about global Git aliases. You can add repo specific Git aliases too. Sometimes they are useful to override the global alias. To add repo specific alias, just edit
.git/config in the repo where you want to add the alias, and follow the same syntax, or add using
git config without
That’s it for now. Add aliases to your Git config file as you need them to make your Git experience simpler, easier, faster and clean. Hope you find it useful in your daily Git usages.