Awesome Git Aliases

By  on  

Git is an amazingly powerful tool. It can keep track of all the code you write, let you organize your work into different branches, help you seamlessly work with other developers, and even let you time travel and make changes.

But wouldn't it be awesome if Git could do more? What if you could customize it with your own commands, making it do anything you can imagine?

In this three-part series, I show you exactly how to do that. You'll learn how to create custom Git aliases you can use to run any command you'd like. I'll also show you many useful commands you can add to Git to make it even more useful.

How to Alias Commands

Before we go any future, just what is an alias? An alias is a shortcut for running another Git command. You can set alias just like you would any other configuration value.

For example, let's say you often mistype git status as git stats, and you wanted git stats to run the same command. You could do that by setting the alias.stats value to status like this.

git config --global alias.stats status

Boom! Just like that, you've created an alias.

Just like other configuration values, you can also open up your ~/.gitconfig file and add aliases there as well. This does the same thing as the command above.

[alias]
  stats = status

Shortcuts

You likely use Git every day. Commands like git status, git add and git commit are awesome.

But ugh. All that typing. Who has time for that?

Some of the most useful aliases you can set up for Git are one-or-two-character shortcuts to common commands.

[alias]
  a = add
  b = branch
  c = commit
  cl = clone
  co = checkout
  cp = cherry-pick
  m = merge
  p = push --follow-tags
  pu = pull
  r = reset
  s = status 

These aliases can be huge time savers. Instead of typing git status, you only need to type git s. Instead of git add ., you can type git a .. It may sound trivial, but give it a try, and I promise you won't go back to typing out the full words.

Force Push

Sometimes, you need to push changes up to a remote repository and overwrite files. You may be used to doing that with git push --force.

But what happens if somebody else has already pushed changes to the same branch? Your command will wipe out their commits. That's no good.

Git has a safer way to push changes and overwrite your commits. Instead of using the --force flag, you can use --force-with-lease. This flag will prevent you from accidentally overwriting somebody else's commits. The way this works is a bit magical, but you can trust that it does.

You can write a force-push alias for this:

git config --global alias.force-push "push --force-with-lease"

Now you can run git force-push, which is a bit easier to remember. Of course, in the interest of saving a few keystrokes, you can also add a short version of this alias.

git config --global alias.fp force-push

Please note, you can only alias an alias in Git 2.20+.

Shell Aliases

There's another powerful addition to Git Aliases you can use: shell aliases. If you add a ! in front of an alias's value, you can run any shell command. For example, you can add the following alias to print out hello every time you type in git hello.

git config --global alias.hello "!echo hello"

Dad

You know 'em, you love 'em—dad jokes. When you accidentally mistype git add as git dad, it's only appropriate that Git responds with a dad joke.

Note: I didn't come up with this idea. The earliest reference I could find to this idea a post on Reddit.

git config --global alias.dad '!curl https://icanhazdadjoke.com/ && echo'

Now why you accidentally type git dad, you'll get a gem like this.

Why was the robot angry? Because someone kept pressing his buttons!

Classic.

That's All for Now

That's for sticking with this article to the end! Hopefully, you've learned how to set up your own aliases, and you found some of the ones I included useful.

In the next part of this series, we'll be taking a much deeper dive into building custom Git commands that will speed up your workflow. Until then, have fun!

Landon Schropp

About Landon Schropp

Landon is a developer, designer and entrepreneur based in Kansas City. He's the author of the Unraveling Flexbox. He's passionate about building simple apps people love to use.

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • By
    Display Images as Grayscale with CSS Filters

    CSS filters aren't yet widely supported but they are indeed impressive and a modern need for web imagery.  CSS filters allow you to modify the display of images in a variety of ways, one of those ways being displaying images as grayscale. Doing so requires the...

  • By
    Create Keyboard Shortcuts with Mousetrap

    Some of the finest parts of web apps are hidden in the little things.  These "small details" can often add up to big, big gains.  One of those small gains can be found in keyboard shortcuts.  Awesome web apps like Gmail and GitHub use loads of...

Discussion

  1. Dad joke is the best!!

    • Lqndon

      That one always makes me smile.

  2. Neil

    Some I use:

    alias nah="git reset --hard; git clean -df;"
    alias gac="git add . && git commit -a -m "
    alias wip="git add . && git commit -a -m 'WIP'"
    • Lqndon

      Ohhh, I especially like wip. Thanks for sharing!

    • Landon

      That looks like a solid list.

  3. I’ve been using the git lg alias from https://coderwall.com/p/euwpig/a-better-git-log for years, they keep updating it and making it better too.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!