2 min read

Fast commit with Git Aliases

Use Git Aliases to reduce the amount of keystrokes you need to take with Oh My Zsh
Fast commit with Git Aliases
Photo by Mohammad Rahmani / Unsplash

As a developer, when working with code we often collaborate, thus requiring the need of a code repository such as GitHub. Me myself for example, I must push my changes I make to the Dapr JS SDK.

Now have you ever been bother by how many times you must do the following.

# One of the below
git add -A 
git add <files>
git rm

# Commit message
git commit -s -m"MSG"

# Push
git push

Just the above takes a lot of typing work, especially when you need to follow the rule of committing each "remarkable" change and not the entire tree of changes to be able to utilize the rollback feature correctly.

Installing Oh My Zsh

💡 for the full installation of your dev environment, feel free to check my up-to-date gist.
# Update
sudo apt update -y

# Install ZSH
sudo apt-get install zsh

# Install Oh-My-Zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# Install Oh-My-Zsh Spaceship Theme
git clone https://github.com/denysdovhan/spaceship-prompt.git "$ZSH_CUSTOM/themes/spaceship-prompt" --depth=1
ln -s "$ZSH_CUSTOM/themes/spaceship-prompt/spaceship.zsh-theme" "$ZSH_CUSTOM/themes/spaceship.zsh-theme"
ZSH_THEME="spaceship"

# Config Oh-My-Zsh Theme and enable waiting dots
COMPLETION_WAITING_DOTS="true"
sed -i 's/# COMPLETION_WAITING_DOTS="true"/COMPLETION_WAITING_DOTS="true"/g' ~/.zshrc
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="spaceship"/g' ~/.zshrc

# Add Plugin - Syntax Highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# Add Plugin - AutoSuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# Enable plugins
sed -i 's/plugins=(git)/plugins=(git zsh-autosuggestions zsh-syntax-highlighting)/g' ~/.zshrc

# Set ZSH as the default shell
chsh -s $(which zsh)

# Restart terminal
exec zsh

Aliases

Introducing aliases! When you work with Oh My Zsh git, you can utilize the aliases it has built in. This reduces the above to:

# Status
gst

# Add all files
gaa

# Add existing files
gcasm "My commit message"
gp

Which brings us from 34 keystrokes to 7! Imagine doing that on 100 commits, then we go from 3400 keystrokes to just 700. It's a small optimization, but it makes an enormous difference.

Another great cheat sheet to remember this:

git-aliases