mgewcli - maximizing git efficiency with CLI

March 19, 2024

Dictionary photo by <a href="https://unsplash.com/@rvignes">Romain Vignes</a>
Dictionary photo by Romain Vignes

A tale of efficiency - skip

It was a typical work day, we had a little pair programming session, where I was trying to explain to my colleague some deployment processes. In the realm of remote work such sessions, reliant on screen sharing, are prone to hiccups. Connection might get unstable, so the video might lag a bit. It’s usually better when you take this into account, and try to explain things slowly, especially when you’re about to teach something. That time I totally forgot about this rule...

I launched like a rocket, I took every shortcut that I could, making my teammate totally confused. It was intelligible... What possibly can you understand from this?

gbD develop
gco your-feature-branch
gcb develop
gpsupf

I quickly fixed my little mistake, but this made me realise I can share some git efficiency tips that I think many developers already have at the tips of their fingers. If you’re curious what does that mean ☝️, read on.

Git GUI vs terminal

Before we begin, let’s put a little disclaimer right here. I use git via CLI. Chances are you might see that as a relict and use a GUI client. That’s ok and it doesn’t mean this article is not for you :)

While GUI apps are mature now and can offer some benefits over the CLI, I think it’s still beneficial to know how to write, rather than click stuff. Some pros of such approach:

  • speed: it’s a muscle memory, keyboard > mouse
  • scripting: enjoy the power of automation with simple scripts
  • cross-platform consistency: whether you jump on Linux, macOS or connect to remote location via SSH
  • hackerman feeling: impress your colleagues, if you’re into it

And the list goes on...

oh-my-zsh

Another heads-up: I use oh-my-zsh. For those who don’t know it - it’s the most popular framework for your zsh config management. It can bring a lot of fun features to your system shell, but let’s focus on the one in particular - git plugin.

Why this topic? Well, zsh has been the default shell on macOS for several versions now (starting from Catalina) and many devs, myself included, adopt oh-my-zsh right after seeing its cool customizable themes in action. If you peek into the default .zshrc config, you'll notice the git plugin is enabled by default, so most likely the whole setup is already there.

oh-my-zsh plugin git

Alright! The plugin is really simple, it just adds a set of handy aliases and functions for git commands, so you don’t have to type the full long form, e.g. instead of writing git push you can just write gp, saving six precious keystrokes.

To give a better idea how does that look in action, here's a glimpse of some real world everyday commands and their abbreviated counterparts. Full list is available at plugin's page. I recommend going through it at some point if you haven’t yet, I’m sure you’ll discover something cool for yourself in there.

abbreviationfull command
ga filename.extgit add filename.ext
gaagit add --all
gco-git checkout -
gcb new-branchgit checkout -b new-branch
gcp c0mm!t5hagit cherry-pick c0mm!t5ha
gc -m “some commit message”git commit -v -m “some commit message”
gcmsg “different command, same result”git commit --message “different command, same result”
gdgit diff
gstgit status
glolagit log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset' --all
gm branchgit merge branch
glgit pull
gpsupgit push --set-upstream origin $(git_current_branch)
grb some-branchgit rebase some-branch

Notice, it’s perfectly fine to add some extra arguments after the aliased command.
I’m especially a fan of gpsup. Typing this command usually means I just finished my current work and I'm ready to share it in PR up for the review. Very convenient!

The learning investment is basically nothing and whenever you find yourself unsure about some specific command there is always a handy way to check what it does:

type gc
gc is an alias for git commit -v

With some aliases you probably gotta ba a little more careful. I can’t say how many times I invoked Ghostscript by mistyping gs instead of gst… Other than that, I had 0 conflicts with it and such error is hardly catastrophic.

There you have it! GLHF with your new super powers!

Want more?

If you liked this post, why don't you subscribe for more content? If you're as old-school as we are, you can just grab the RSS feed of this blog. Or enroll to the course described below!

Alternatively, if audio's more your thing why don't you subscribe to our podcast! We're still figuring out what it's going to be, but already quite a few episodes are waiting for you to check them out.

Blog author: Błażej Lewandowski
WRITTEN BY

Błażej Lewandowski

React Native developer

Happy puzzle phone

More Brains and Beards stories