My Git Aliases

Git is an awesome tool for version control and has been accepted as the industry standard. However, Git commands can be very confusing and difficult to remember. When I first learned about version control I used something called Mercurial, which had simpler and more intuitive commands (generally speaking). In an effort to tame Git and make it a bit more intuitive, I’ve created a few aliases that I think you might find helpful.

What is a Git Alias?

No, a Git alias isn’t when you commit using someone else’s name… although I’ve done that before. 😉 A Git alias is nothing more than a custom git command that can reference a longer git command or even a custom shell command. Put simply, it is a command-line shortcut.

For example, when I want to perform a git checkout, I actually just type git co because I’ve created co as an alias of checkout.

How to Create a Git Alias

So how do you go about creating an alias? The simplest way is to create one via the command line, like this:

git config --global alias.co 'checkout'

Alternatively, you can manually edit your .gitconfig file in your user directory by adding the following:

[alias]
    co = checkout

The [alias] bit shoult only be in there once, but under that you can add as many aliases as you want. All of your aliases should be indented under the [alias] heading with a single tab.

If you want to quickly start editing your .gitconfig file, just run this command:

git config --global -e

My Git Aliases

OK, on to the good stuff. Here are the aliases I use, what they do and a handy copy and paste command for adding the alias to your own .gitconfig file.

co

The co alias is a shortcut for the checkout command.

To create this alias, just run this command:

git config --global alias.co 'checkout'

ci

The ci alias is a shortcut for the commit command. I think of it as the ‘Check In’ command. While there is no checkin command in Git (or Mercurial), there is in SVN and it is the perfect opposite to the checkout command.

To create this alias, just run this command:

git config --global alias.ci 'commit'

st

The st alias is a shortcut for the status command, but with a small twist. I prefer to see the status in a condensed mode, so I add the -s flag to opt for the short display. I also add the -b flag so it always shows me the current branch. If you don’t want those flags set, just leave them off when creating your alias.

To create this alias, just run this command:

git config --global alias.st 'status -sb'

in

The in alias is patterned after the incoming command in Mercurial. Basically, it tells me if there are any incoming commits I should pull down. Ultimately, it is not so easy to git this information from Git without a lot of typing. It is also strangely satisfying to type git in.

To create this alias, just run this command:

git config --global alias.in '!git fetch && git log --oneline --graph ..@{u}'

Note: The ! character at the beginning of a Git alias basically causes the alias to run a shell command. As such, we can really run any command supported by our operating system. In this case, I’m just running two git commands back to back.

out

The out alias is patterned after the outgoing command in Mercurial. Basically, it tells me if there are any outgoing commits that I should push up. Again, it is also just fun to type git out!

To create this alias, just run this command:

git config --global alias.out 'log --oneline --graph @{u}..'

ready

The ready alias is one for which I have to credit my brother David. Basically, it stages all changes and then shows the current status. This is typically what you might do right before a commit, so it is aptly named. This one is also just fun to type!

To create this alias, just run this command:

git config --global alias.ready '!git add --all && git status -sb'

Note: I’ve applied my personal preferences to the status command by adding the -s and -b flags here. Feel free to customize and make it your own!

unstage

The unstage alias will do exactly what you think it does: Unstage any staged changes. While the Git way of doing this isn’t hard, it also isn’t intuitive. I always opt for intuitive where possible.

To create this alias, just run this command:

git config --global alias.unstage 'reset --'

rollback

The rollback alias will undo the last commit. It is patterned after the rollback command from Mercurial. When I first started working with Git, I found myself looking up how to do this all the time. Since Git doesn’t have a rollback command, we can add it ourselves!

To create this alias, just run this command:

git config --global alias.rollback 'reset HEAD~'

forget

The forget alias will make Git forget about one or more files. Again, this is something I used to look up all the time because it isn’t intuitive to do in Git.

This alias isn’t a meant to be used without additional parameters. For example, I wouldn’t just run git forget because it wouldn’t really do anything. However, if I want Git to forget about my huge.log file, I would just run git forget huge.log.

To create this alias, just run this command:

git config --global alias.forget 'rm --cached'

leaderboard

The leaderboard alias is just for fun and credit for this one goes to Kolbe. Basically, it just lists all the authors who have made commits in order by the number of commits they’ve made to the repository. While the number of commits isn’t the best way to judge contributions to a code base, it is a good way to get a feel for activity.

To create this alias, just run this command:

git config --global alias.leaderboard 'shortlog -s -n'

alias

Last but not least, the alias alias is used to list all your aliases! I know that when you have a bunch of aliases it is likely that you will forget about them from time to time. Maybe you need to do something more complicated and know you have an alias to do that, but don’t remember the name of it. Have no fear! Just add this alias and all you need to do to list out your aliases is type git alias!

To create this alias, just run this command:

git config --global alias.alias 'config --get-regexp alias'

Summary

So there you have it, 11 awesome Git aliases you can start using today! If you don’t want to add them all one at a time from the command line, you can just copy and paste this whole block into your .gitconfig file:

[alias]
 st = status -sb
 co = checkout
 ci = commit
 in = !git fetch && git log --oneline --graph ..@{u}
 out = log --oneline --graph @{u}..
 ready = !git add --all && git status -sb
 unstage = reset --
 rollback = reset HEAD~
 forget = rm --cached
 leaderboard = shortlog -s -n
 alias = config --get-regexp alias

Have you already been using Git aliases? If so, let me and everyone else know by sharing the ones that have helped you the most!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.