davit.tech

GIT aliases. Setup and usage guides

21, March 2020 - 3 min read

Backstory ☜

I was helping a colleague of mine to do a rebase with git and after typing git st third time he told me st is not a git command, why are you typing it again and again. I replied to him it's just a habit but on my computer, it's a valid command.

git terminal st command

Intro ☜

To understand git aliases better, first of all, let's define the word alias. According to google alias is an adverb that used to indicate that a named person is also known or more familiar under another specified name and in the world of computing it's an alternative name or label that refers to a file, command, address, or other item, and can be used to locate or access it. And that's also true when we talk about aliases in GIT. Aliases are used to create shorter commands that are mapped to longer ones. They allow us to use fewer keystrokes to execute *large* commands which saves us some(small) time and reduces the probability of typos and errors.


Setup and Usage ☜

There is no special command for aliases in git. Aliases are part of the git config and they can be configured globally(at the operating system level) as well as locally(at the project level). Here is the formula of alias configuration:

$ git config --global alias.*name of the alias* *original command*

*name of the alias* could be st and *original command* status like below:

$ git config --global alias.st status

Here are some other examples of alias configurations grabbed from official docs:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

As you can see there are --global flags in commands above which means that aliases will be stored in operating system level configuration file. In Linux, that file is called .gitconfig and stored in User home directory. Here is the content of the file after configuring aliases.

...
[alias]
      co = checkout
      br = branch
      ci = commit
      st = status

Now we can execute git co BranchA instead of git checkout BranchA or git ci instead of git commit and they will behave the same.

If we remove --global flag when configuring aliases they will be available only inside the repository that we have configured. Local configurations stored in .git/config file under your projects root directory and they will override global aliases. It can be useful if we want to share aliases with our colleagues.

As you can see, GIT simply replaces the new command with whatever you alias it for. However, maybe you want to run an external command, rather than a GIT subcommand. In that case, you start the command with a ! character. This is useful if you write your own tools that work with a GIT repository. Example of that could be aliasing git visual to run gitk:

$ git config --global alias.visual '!gitk'

IMPORTANT!
We can't create aliases to override existing commands and we can't create aliases using other aliases.


Ideas for aliases ☜

Creating 2 or 3 letter aliases for single commands will help us a lot, but configuring aliases for command + argument(s) will help us even more.

Unstaging is another common git operation that we use frequently on daily basis and creating alias for this operation will save us some time too.

Normally we unstage files this way git reset HEAD -- FileA and we want to have an alias called unstage or us. Now let's configure it.

$ git config --global alias.us 'reset HEAD --'

And now we can unstage files more easily.

$ git us FileA

Another idea for an alias is getting the last commit log. Normally we would do git log -1 HEAD, so the alias setup will be git config --global alias.last 'log -1 HEAD' and now git last will show us the last commit information. Very intuitive, isn't it?

The last but not least alias idea is the commit command with the --amend flag. And an alias for this could be cia or ca and instead of typing git commit --amend or git ci --amend we will type git cia.


Conclusion ☜

Aliases are a powerful tool that creates shortcuts to frequently used git commands, it will increase your productivity, speed, and efficiency when working with git repositories.

And if you have any other interesting ideas for aliases, please comment them below. 👇

This might interest you.