dark mode

Increase productivity using Git Aliases.

Increase productivity using Git Aliases.

You can use Git with graphical interfaces, but the only way to have full control and to know exactly what you are doing is to use the command line.

The graphical interfaces come with convenient visualisations. However, many times they come with additional bugs and simple commands are doing, not the way the user might want. We can observe this on the popular SourceTree app, which does simple actions with multiple commands. This is not bad, but is not user intended, and sometimes it might follow to code lost.

To write the same Git commands every time is not a pleasant task. Thankfully, Git gives the way to short and chain them.

We can represent everything after the git command as shot as a single letter. For example, simple command but used constantly is git status. We can replace this command with git s. This means we give alias s to the status command.

Using the git config... command you can create aliases. Below is an examples:

  git config --global alias.c 'commit -m'
  git config --global alias.co checkout
  git config --global alias.f fetch
  git config --global alias.a add
  git config --global alias.d diff
  git config --global alias.ps push
  git config --global alias.pl pull
  git config --global alias.s status
  git config --global alias.l "log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %Cblue%an%Creset' --abbrev-commit --date=relative"

The last command gives you the table like visualisation of the commits. For more details, you can see the documentation for the git log command.

Git stores all aliases in the .gitconfig file. On windows you can find the file in the C:\Users\<your user name> folder.

If you execute all the commands above, you will find the aliases represented like this:

[alias]
  c = commit -m
  co = checkout
  f = fetch
  a = add
  d = diff
  ps = push
  pl = pull
  s = status
  l = log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %Cblue%an%Creset' --abbrev-commit --date=relative

You can add new aliases directly into the file if the syntax is correct.

We can execute more sophisticated command or even chain a couple of them with exclamation mark syntax. This will give you the ability to run external commands rather than sub-commands.

Often we use fetch and then status commands together. We can create alias which combine to commands like this:

git config --global alias.fs '!git fetch && git status'
# or which the aliases
git config --global alias.fs '!git f && git s'

By now, we can see how powerful aliases are and how could make our lives easier and more productive.

Related articles

© 2021 All rights reserved.