Achieving Efficiency Through Git Aliases

More Than Just Shortening Git Commands

Quinn Vissak
Jun 11 · 5 min read

TL;DR — Save time by automating lengthy processes

Git aliases are more than just shortening git commands — aliases in the file grant you the ability to execute scripts! With this, daily tasks that used to take minutes can shrink to seconds.

Now, instead of opening a new tab in my browser, going to GitHub, finding the repository I just pushed to, creating a pull request, and finally selecting the target fork/branch, I created an alias. executes a script that automatically opens a new tab in Google Chrome with a pull request from my working branch to master. Similarly, I use to pull changes from the upstream repository and rebase them onto my working branch. adds, commits, rebases, and squashes, my unstaged changes into the previous commit.

Given the high frequency of such use cases, these three commands alone give me back 10–15 minutes daily that I apply toward writing better software.


This is not an article that just explains how to make a git alias. Rather, we will also dive into the aliases described above that I have created to automate monotonous tasks, shortening execution time from minutes to merely seconds.

I am a software engineer who can be working on any of ten to fifteen different repositories on a given day. Many are designed in similar stacks, so I often absentmindedly forget which repository I am contributing to. The repos are in multiple organizations to which I do not necessarily have write access. Sometimes different projects have different branching schemes, so is not always trunk. Before using aliases, I found myself spending up to five minutes just trying to make a simple pull request!

What is a Git Alias?

An alias in git is as straightforward as it sounds. A configuration file named lives in your directory (on MacOS) and contains what is basically a dictionary of key-value pairs where the key is the alias and the value is the command or set of commands the alias will execute. More on setting up your file for the first time can be found in git’s documentation.

Why use Git Aliases?

A lot of people use an alias to shorten commands they commonly use,

git config --global "checkout"

or to create an alias of a common command that uses really long flags,

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

or to rename unintuitive commands,

git config --global alias.unstage "reset HEAD --"

but aliases can be so much more!

Fully Leveraging Git Aliases to Maximize Your Time

Here is a sample file below. It does not have many commands, but the commands it does have save me a lot of time.

name = ******
email = ************@**********.com
squash = ! /Users/******/
co = checkout
cob = checkout -b
rename = branch -m
pu = ! git fetch upstream && git rebase upstream/master
pr = ! /Users/******/
undo = checkout --
unstage = reset HEAD --
last = checkout -

A couple things to note:

  • The is necessary on lines where I am executing a script (some scripts are inline below).
  • Scripts that I wrote in a file are referenced by their absolute path, instead of their relative paths. Relative paths can cause a problem depending on where you clone your repositories on your machine.
  • In , must be set to the url (ending in ) of the repository you forked your project from in order to use the commands I discuss below.
  • I assume is the same as your GitHub username (this may not be true, feel free to change it if necessary).

git pr

The script below automates the pull request creation process by finding the upstream repository from which I originally forked my project, finds my current working branch, and opens a PR in Google Chrome pointing to the correct target branch. In order to use this script, you will need to have Google Chrome and Homebrew installed. I am certain another Chrome utility can be found and the script can be tweaked for non-MacOS.

#!/bin/bash# Install chrome-cli using Homebrew the first time you run this
# script only, this will enable you to open Google Chrome from CLI.
if ! brew ls — versions chrome-cli > /dev/null; then
brew install chrome-cli
# Get the upstream URL (repo from which you forked) you set using
# `git remote add upstream<user>/<repo>.git`.
upstream=$(git config --get remote.upstream.url)
# Get the name of the branch you are currently on.
branch=$(git rev-parse --abbrev-ref HEAD)
# Trim `.git` from the upstream URL.
# Build the URL GitHub uses to create a pull request.
# Feel free to change `QA` to `master` or to whichever branch you
# frequently merge.
# Open the URL we constructed in a new tab in Google Chrome.
chrome-cli open $uri

There are a few different ways one can write this script and achieve the same outcome, but they usually involve two commands or one command that has a couple different lengthy flags. In a nutshell, this will pull the latest code changes from your upstream repository and rebase the changes from its master branch onto your current working branch.

git fetch upstream && git rebase upstream/master

I am obsessed with having a clean git commit history. This command replays your unstaged changes onto the previous commit, keeping the same commit message, and force pushes to your branch. This overwrites the commit history. I often think I am done, push up my code — just to review the pull request I created and see there is a silly mistake. This command enables one to make a change quickly and push it up. It keeps the same commit style without having to add, commit, push, rebase, squash, delete the dummy commit message, and push changes to the branch.

#!/bin/bashecho "Adding all of your unstaged changes into your previous commit on the server."# Add all changes (tracked and untracked) to staging directory.
git add .
# Commit changes in staging directory into previous commit, keeping
# the same commit message.
git commit --amend --no-edit
# Force push (overwrite) your branch, fail only if new changes were
# pushed by someone else and you haven't rebased yet.
git push --force-with-lease
echo "Complete! Changes squashed and merged."exit 0


Reference the git documentation on aliases if you want to know more about things not discussed in this article or if you have questions about some of the syntax used to assign aliases. Please comment below with ideas you have to automate your workflow!

DISCLOSURE STATEMENT: © 2019 Capital One. Opinions are those of the individual author. Unless noted otherwise in this post, Capital One is not affiliated with, nor endorsed by, any of the companies mentioned. All trademarks and other intellectual property used or displayed are property of their respective owners.

Capital One Tech

The low down on our high tech from the engineering experts at Capital One. Learn about the solutions, ideas and stories driving our tech transformation.

Quinn Vissak

Written by

Backend Senior Associate Software Engineer at Capital One — passionate about utilizing the best software practices to make systems and teams more efficient.

Capital One Tech

The low down on our high tech from the engineering experts at Capital One. Learn about the solutions, ideas and stories driving our tech transformation.