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
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Sexy Album Art with MooTools or jQuery

    The way that album information displays is usually insanely boring. Music is supposed to be fun and moving, right? Luckily MooTools and jQuery allow us to communicate that creativity on the web. The XHTML A few structure DIVs and the album information. The CSS The CSS...

  • By
    WebKit Marquee CSS:  Bringin’ Sexy Back

    We all joke about the days of Web yesteryear.  You remember them:  stupid animated GIFs (flames and "coming soon" images, most notably), lame counters, guestbooks, applets, etc.  Another "feature" we thought we had gotten rid of was the marquee.  The marquee was a rudimentary, javascript-like...

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!