DEV Community

Cover image for Improve Git CLI Efficiency with Oh My Zsh
Sean Kegel
Sean Kegel

Posted on • Originally published at seankegel.com on

Improve Git CLI Efficiency with Oh My Zsh

Git aliases

Do you use Git in the CLI or GUI? I was a heavy GUI user earlier in my career and I still do some quick operations using VSCode, PhpStorm, or Fork. At a previous company, I was forced to use GitKraken because it helped prevent developers from making Git errors. GitKraken is a great product and using the GUI tools definitely helped me become more comfortable using Git and learning the various commands. It really helps you visualize the changes being made to your repository.

However, as you start to progress in your career, you may find yourself gravitating more to the terminal to do a quick git pull, git push, or git commit. Or maybe you’re already an experienced Git user and do all your work in the CLI.

Using Git aliases in the CLI can make you even more efficient. That’s where Oh My Zsh comes in. Oh My Zsh is a framework for managing your Zsh configuration. Zsh is a shell similar to Bash but with some nice additional features. You can navigate to directories without typing cd. It offers spelling correction, plugins, themes, etc. One of my favorite features is the previous command search. I can start typing a command, then push “up” to see previous commands I’ve used starting with the text I already typed. If you are using a Mac, you’ve likely already have Zsh installed and running as the default. Otherwise, you can actually refer to the Oh My Zsh docs to install it.

Once you are running Zsh, the next step is to install Oh My Zsh. This can be done using a simple curl or wget command:

sh -c$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
Enter fullscreen mode Exit fullscreen mode

Once installed, you’ll want to edit your .zshrc file, and look for the plugins array and make sure git is added. It should look something like the following:

plugins=(
  git
)
Enter fullscreen mode Exit fullscreen mode

Once that is in place go ahead and source your .zshrc file, or just restart your terminal.

Now, we have a ton of aliases for common Git commands.

Here’s some of my favorites:

Commits

gcmsg ‘Commit messsage’ # Equivalent to git commit -m ‘Commit message’
Enter fullscreen mode Exit fullscreen mode

Amend all files to previous commit

Sometimes I add a commit and realize I forgot to modify a file, so I make the change and then I want to amend the change into the previous commit.

gcan! # Equivalent to git commit — verbose — all — no-edit — amend
Enter fullscreen mode Exit fullscreen mode

Work in progress

This creates a commit with the message --wip-- [skip ci] with all your currently modified files.

gwip
Enter fullscreen mode Exit fullscreen mode

Pushing and Pulling

gl # Equivalent to git pull

gp # Equivalent to git push
Enter fullscreen mode Exit fullscreen mode

Git force push with lease

You always need to be careful with force pushing a branch. One of the best things to do is force push with lease. This means the force push will fail if someone else has added additional commits to the remote branch.

ggfl # Equivalent to git push — force-with-lease
Enter fullscreen mode Exit fullscreen mode

Branches

The aliases for creating and checking out branches are some of my most used.

gcb my-new-branch # Equivalent to git checkout -b my-new-branch

gco - # Equivalent to git checkout — which checks out the previous branch

gcm # Equivalent to git checkout main
Enter fullscreen mode Exit fullscreen mode

Rebasing

I use tend to use git rebase pretty often. It’s definitely something that using a GUI really helped me understand what it was doing. I use git rebase to add my commits onto another branch. I also use git rebase -- interactive HEAD~3 to reorder and modify commits, in this case, HEAD~3 refers to the last three commits.

grb {branch} # Equivalent to git rebase {branch}

grbi HEAD~3 # Equivalent to git rebase — interactive HEAD~3
Enter fullscreen mode Exit fullscreen mode

Diff and Log

gd # Equivalent to git diff
Enter fullscreen mode Exit fullscreen mode

Git log

When I want to quickly see a one line version of all the previous commits, I use the glog alias.

glog # Equivalent to git log — oneline — decorate — graph
Enter fullscreen mode Exit fullscreen mode

For a complete list of aliases available in the plugin, click here.

If you are using a different shell, like bash or fsh, there may be similar plugins available, but if not, you can still easily create you own aliases in your config file, like .bashrc. Or maybe there is a common Git commands (or CLI commands in general) that you use often enough to warrant an alias.

alias gc=’git commit -m
Enter fullscreen mode Exit fullscreen mode

Thanks for reading! I hope this will help you improve you Git CLI skills. Let me know if you have any other common Git aliases you use, either from the plugin or custom aliases you have created.

Top comments (0)