DEV Community

Cover image for ⚡ How to become faster in Git CLI
Cristian Prochnow
Cristian Prochnow

Posted on

⚡ How to become faster in Git CLI

👋🏻 Hey, there

First of all, get your favorite drink, prepare your .gitconfig, and come with me through this huge valley called "Git"...


Git is constantly present in our routine as developers, and a good approach to use this tool is essential. For this case — and any else — there is Git Alias.

Alt Text

🤔 What is it?

A Git Alias is simply as a shortcut to other command used in Git CLI. In one fast and abstract comparation, the complete command from Git looks like the functionality of Copy something using the right button from mouse, and a Git Alias is the Ctrl + C shortcut.

⁉ Why?

Because you are able to save time using this shortcuts, and with specifics settings some commands that are naturally long will be reduced to one keyword.

With a simple example, you will be able to turn this:

git remote add origin https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

Into this:

git remo https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

⚠ In this case, I have set remo as a alias from remote add origin, but the name of it can be any word you wish.

🚀 Here we go...

Find the configuration file

First of all, it is necessary to find the configuration file from Git, in your machine.

Windows

# To set the config for one user only
C:\Users\{your_user}\.gitconfig

# To set the config for all the users from machine
C:\Program Files\Git\etc\gitconfig
Enter fullscreen mode Exit fullscreen mode

Unix

# To set the config for one user only
~/.gitconfig
# or
~/.config/git/config

# To set the config for all the users from machine
/etc/gitconfig
Enter fullscreen mode Exit fullscreen mode

Set the Aliases

Open the file with an IDE by your preference, and start to set shortcut for commands you need. Before set the first keyword, type [alias], indent, and then you are able to set shortcuts. For example:

[alias]
  ad = add .
  remo = remote add origin
  rem = remote add
Enter fullscreen mode Exit fullscreen mode

⚡ Aliases I use

To save time, you can copy and paste the aliases I use, and then change each of them by your preference.

[alias]
  ad = add .
  remo = remote add origin
  rem = remote add
  ci = commit -m
  pm = push origin master
  po = push origin
  plm = pull origin master
  pl = pull origin
  co = checkout
  cm = checkout master
  cb = checkout -b
  st = status -sb
  sf = show --name-only
  lg = log --pretty=format:'%Cred%h%Creset %C(bold)%cr%Creset %Cgreen<%an>%Creset %s' --max-count=30
  incoming = !(git fetch --quiet && git log --pretty=format:'%C(yellow)%h %C(white)- %C(red)%an %C(white)- %C(cyan)%d%Creset %s %C(white)- %ar%Creset' ..@{u})
  outgoing = !(git fetch --quiet && git log --pretty=format:'%C(yellow)%h %C(white)- %C(red)%an %C(white)- %C(cyan)%d%Creset %s %C(white)- %ar%Creset' @{u}..)
  unstage = reset HEAD --
  undo = checkout --
  rollback = reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

🏁 Conclusion

Git Alias is a huge friend of our productivity, and surely will help you in your daily commits. For more information about this tool, check the official documentation, or the complete guide of Git.

This code I use is an adaptation from this one.

Top comments (0)