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.

This article is part of the No Bullshit Git Free Intro Course. If you're interested in Git and you want to catch the other lessons, be sure to sign up for the free course.

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!

If you enjoyed this article and want to learn more about Git, be sure to check out the No Bullshit Git Free Intro Course.

Landon Schropp

About Landon Schropp

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

Recent Features

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Full Width Textareas

    Working with textarea widths can be painful if you want the textarea to span 100% width.  Why painful?  Because if the textarea's containing element has padding, your "width:100%" textarea will likely stretch outside of the parent container -- a frustrating prospect to say the least.  Luckily...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

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!