Let Git Aliases Boost Your Productivity
By: Luke Hollenback
Tags: Tips, Development, Productivity, Programming, Git

Useful Aliases

There is an unlimited number of possibilities for aliases. What’s important is that you define the ones that are valuable for your specific day-to-day workflow.

Below are a number of aliases that I have defined for my personal workflow. I find that they simplify my Git usage quite a bit. You may or may not feel the same way.

Checkout branch

This alias is as simple as it gets. It is a keystroke-shortener for checking out a new branch.

Alias: co = checkout

Example Usage: git co my-fancy-branch

Stage and commit changes

Most of the time, when I am wanting to commit changes, I just want to stage and commit everything that I have done. This alias lets me do so.

Alias: ac = !git add -A && git commit

Example Usage: git ac -m "Some Commit Message Goes Here"

Create new branch

Creating a local branch, and then a remote branch to push to late, almost always stops me in my tracks when I try to push my changes and realize that I can’t remember how to create the remote version of my new branch. For this reason, I find it best to just do both in a single line. Thus, this alias creates a new local branch off of the current working branch, and then immediately connects it to a new remote branch.

Alias: create-branch = !git checkout -b $1 && git push -u origin

Example Usage: git create-branch “my_new_branch”

Notice the $1 in this alias. This interpolates the first parameter — in this case, the desired branch name — that was passed to the alias into the command that gets executed.

Sync Branch w/Dev

I try to sync up my feature branches with the dev branch that other developers on my team are pushing to regularly. This helps me avoid accidentally breaking other features, tests, and so on, without noticing — and vice-versa. This alias quickly syncs up my current working branch for me.

Alias: sync = !git pull origin dev

Example Usage: git sync

List branch’s changed files

When I am wrapping up work on a feature branch and about to create a pull request to get it into dev , I like to take a final look at all of the files that have been changed. This helps me ensure that, for example, I’m not introducing new code smells into the codebase. This alias will list out the names of all files that have been changed in my current working branch.

Alias: get-changes = !git diff — name-only origin/dev

Example Usage: git get-changes

Delete old local branches

On my team, whenever we merge a pull request into dev , we delete the relevant feature branch. This helps to keep our activity clean — thus making it easy to see what’s in progress at any given moment.

Because we do this, I can very quickly clean up local clones of old branches in my environment by deleting the ones that have already been deleted on our remote. I actually find that doing this regularly speeds up some other Git operations I perform, though I do not know why.

Alias: clean-branches = !git remote prune origin && (git branch -vv | grep ‘origin/.*: gone]’ | awk ‘{print $1}’ | xargs git branch -D)

Example Usage: git clean-branches

This alias is probably my favorite of the bunch — it simplifies a fairly complex pipeline of commands, and helps keep my environment pristine.