DEV Community

Cover image for πŸ•΅οΈβ€β™‚οΈ Learn git alias and boost your productivity
Leonardo Montini for This is Learning

Posted on • Edited on • Originally published at leonardomontini.dev

πŸ•΅οΈβ€β™‚οΈ Learn git alias and boost your productivity

Setting up an Alias

Repeating the same git commands over and over again can be such a waste of time! And some of the most powerful ones are usually quite long and impossible to memorize.

That’s why aliases have been introduced!

Setting up an alias is really simple, just open up a terminal and type



git config --global alias.[commandName] [long command]


Enter fullscreen mode Exit fullscreen mode

For example: git config --global alias.st 'status'

In this case, if I call git st, it will give me the result of git status.

git st output

Ok, this was easy, but what about long commands such as this one?



log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit



Enter fullscreen mode Exit fullscreen mode

You don’t want to write it every single time, right?

Just run this:



git config β€”global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"



Enter fullscreen mode Exit fullscreen mode

And you never have to remember this long command again!

Look at how cool and colorful this log is, by just using git lg:

git lg output

Understanding Aliases

If with these two examples you agree with me that aliases are cool, let me give you some more information you should have, in order to use aliases mindfully. Later on, I’ll also share with you a list of other smart aliases you might find useful.

You can find everything in the video down below, where I also show:

  • How to easily edit aliases without setting them from terminal
  • How to use the bang operator ! (aka exclamation mark)
  • How this this weird syntax is useful: "!f(){ [some commands here] }; f"
  • A list of cool aliases to set up for you

See aliases in action

You can watch the video on YouTube, or directly here from the embedded player:


If you have some cool aliases to share, feel free to drop them here in a comment, thank you!


Thanks for reading this post, I hope you found it interesting!

Do you like my content? You might consider subscribing to my YouTube channel!
You can find it here:
YouTube

Feel free to follow me to get notified when new articles are out ;)

Top comments (13)

Collapse
 
mfurquimdev profile image
Mateus Furquim

I do have an alias that I use often. I call it git ld, which is short for git log diverged.

git config --global alias.ld "log --oneline --graph --decorate main $(git branch | grep "\*" | cut -d " " -f 2) $(git merge-base main $(git branch | grep "\*" | cut -d " " -f 2))^\!"
Enter fullscreen mode Exit fullscreen mode

Suppose your git flow consists of creating a branch from main, implementing some features, and merging it back to main.

In this scenario, you want to know if your branch has diverged from main.

Let's say git log shows something like this:

$ git log --oneline --graph --decorate --all
* c36d102 (main) New commit on main
| * f5003c8 (HEAD -> develop) New commit on develop
|/
* c484b40 Where it has diverged
* ec3124d Initial commit
Enter fullscreen mode Exit fullscreen mode

This output might be alright for few commits, but if there are more than a dozen, it starts getting polluted. The output of git ld is this:

$ git ld
* c36d102 (main) New commit on main
| * f5003c8 (HEAD -> develop) New commit on develop
|/
* c484b40 Where it has diverged
Enter fullscreen mode Exit fullscreen mode

OBS: I took most of this code from a StackOverflow answer that I cannot find. If I do find, I will edit to include the source.

Collapse
 
balastrong profile image
Leonardo Montini

Looks really useful, thanks for sharing!

Collapse
 
puppo profile image
Luca Del Puppo

I have seen these aliases before 😜

Collapse
 
balastrong profile image
Leonardo Montini

Yep, talking with you the other day gave me the idea for making this video aaaand I may or may not have borrowed an alias or two from the ones you showed me :D

Actually, most of the ideas for videos and articles come directly from chatting with colleagues and friends, so, thank you! πŸ˜‰

Collapse
 
puppo profile image
Luca Del Puppo

If you wanna new ideas for your content or you wanna help with something you know where I am! πŸ™‚

Collapse
 
yogeshktm profile image
yogeshwaran

Read about aliases before and never tried using in real time.

After reading this article have to try this immediately :)

Collapse
 
balastrong profile image
Leonardo Montini • Edited

Let's go! πŸš€

Let me know how it goes or if you find some aliases you enoy particularly :P

Collapse
 
jonathanpwheat profile image
Jonathan Wheat • Edited

I create shell aliases for everything (lazier or more efficient? you decide haha)
For example, created the alias glg for git lg

Collapse
 
balastrong profile image
Leonardo Montini

Basically something like this:

alias meme

Collapse
 
peterwitham profile image
Peter Witham

Thank you, I normally create my Git aliases in my shell config, I never knew you could put them in Git configuration.
Thanks.

Collapse
 
balastrong profile image
Leonardo Montini

Glad to hear you found my video useful
!
Thank you so much for letting me know and enjoy your new fast way of editing aliases :D

Collapse
 
rabeehco profile image
Rabeeh Ebrahim

Thank you for this helpful tip. I am going to save a lot of time πŸ˜‰

Collapse
 
yuridevat profile image
Julia πŸ‘©πŸ»β€πŸ’» GDE

Nice, thanks for sharing.