What is a Git alias?

If you're unfamiliar with Git's "alias" feature, it provides a way to create shortcuts for other Git commands, which can save you a lot of time. They’re easy to setup and maintain too. Let's see how.

What is a Git alias?

If you're unfamiliar with Git's "alias" feature, it provides a way to create shortcuts for other Git commands, which can save you a lot of time. They’re easy to setup and maintain too.

Using Aliases for Shortcuts

You can create aliases for short commands, like assigning "checkout" to "co":

git config --global alias.co checkout

Or longer commands, like this one that displays a unique log view:

git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"

And what if you’re the sole developer of some project and you’d like to be able to add, commit and push your files in one go?

You can do that too:

git config alias.shove '!git add . && git commit --allow-empty-message -m "" && git push'

I’d recommend that last one only on repos you maintain by yourself, since you’ll want to be more careful on team projects. That’s why I omitted --global.

Alternatively, you could add a $1 place-holder to force a commit message.

git config --global alias.cshove '!git add . && git commit -m "$1" && git push && :'

(That final colon is a bit of a hack but there’s a reason for it.)

Any aliases you configure are added into your:

  • global .gitconfig file if you use --global, or
  • a single repository’s .git/config file if you use --local or just omit the modifier (learn more here)

Here’s what it looks like in the config file itself. You can edit the file manually if you’d like… the git config alias line I used above is just for convenience.

    co = checkout
    hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
    shove = !git add . && git commit --allow-empty-message -m \"\" && git push
    cshove = !git add . && git commit -m \"$1\" && git push && :

Use them like you’d use any other git command, by calling git shove for instance.

Using Aliases for Typos

There’s another good use for aliases too – to fix common typos.

By default, Git will make helpful suggestions when it catches a typo. If the typo’s not too bad it’ll usually make a single suggestion and, in my experience, it’s nearly always right. Which makes it a little annoying that it can’t just pop that single suggestion onto the prompt so I can execute it!

In reality, it’s not all that safe to just have Git blindly execute its suggestion, but you can do it if you like living dangerously. The number is in tenths of a second, so 30 tells Git to execute the suggestion automatically in 3 seconds. You can increase the number if you want a longer opportunity to cancel the command.

> git config --global help.autocorrect 30
> git pul
git: 'pul' is not a git command. See 'git --help'.
Did you mean one of these?
> git pulll
WARNING: You called a Git command named 'pulll', which does not exist.
Continuing under the assumption that you meant 'pull'
in 3.0 seconds automatically...
Already up-to-date.

But wait, there’s more!

What if you have a tendency to type git pul all the time and don’t want Git providing its top 2 guesses? You can setup aliases for common misspellings, and now you have fuller control over what command runs when.

> git config --global alias.pul pull
> git pul
Already up-to-date.
> git config --global alias.puhs push
> git puhs
Everything up-to-date

What’s Next?

I’m going to add some aliases for useful commands and other common typos in a GitHub repo so I don’t lose them… and maybe you’ll find them useful too!

If you’d like to use them, you don’t have to copy/paste anything. Instead, clone the repository and reference it from within your existing .gitconfig file, which has the added benefit of leaving your existing aliases and other settings untouched. Add the following section to your .gitconfig file, where /your/path is wherever you cloned the repo to.

    path = "/your/path/git-alias-template/alias-shortcuts.gitconfig"
    path = "/your/path/git-alias-template/alias-typos.gitconfig"
    # etc...

The include section imports settings from other files and merges their functionality without affecting the original config file.

You can read more about aliases here:

Do you have any helpful aliases of your own, or any cool Git tips you’d recommend? I’d love to hear about them!

And if you’d like to learn even more cool features of Git, check out the GitHub Cheat Sheet… you’ll need a few, um, hours to check it all out…

(photo by Brett Jordan from Pexels)