Using Git Aliases

December 20, 2014

I often spend my days writing code and committing it to a git repository. I love git. It makes me fearless and leaves me wondering how I did anything before we met. I hope you at least like git a little bit.

However, git isn't known for its beautiful interface. Certain commands are oddly named, have a long list of options, and are capable of doing multiple, seemingly unrelated actions.

There is hope. Git can learn new commands, allowing you to define a more convenient interface that fits nicely into your workflow. Lengthy commands that are difficult to remember can be given simple names, and common commands that are used frequently can be aliased with shortcuts. After hours at a keyboard, those keystrokes will really add up, man.

I know how exciting this sounds, so let's get to it.

show me the money

Remember how I mentioned commands aren't always easy to remember? Let's look at how to define your first alias.

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

Neat. Take a moment for that to sink in, and try running git st on one of your repos. If we had left out the --global flag, this command would only work in your local repo, but it seems more likely that you would want this shortcut for all your projects.

Now, when we type git st, git will do the exact same action as if we had done git status -sb. This command is one of my favorites, since it can be typed more quickly than git status and the output is a bit nicer.

.gitconfig

Despite popular belief, computers are not magical devices. When we teach git a new trick, it needs to hold onto that information somewhere. In fact, it does so in a small file. Let's go track it down.

$ cd ~
$ ls -a

At this point, I'm sure you're looking at a number of files, but the one we care about is called .gitconfig. If you open that file in your favorite text editor, you should at least see a couple lines that look like this.

[alias]
  st = status -sb

We could have manually edited this file, and git would have been perfectly happy. In fact, let's try that out.

Right below the st definition, scribble down a line like this and save the file. Make sure to use a single tab character for the indentation.

[alias]
  st = status -sb
  co = checkout

Now, when you run something like git co master, git will peek at this file, find out you really meant git checkout master, and take care of business.

more examples

[alias]
  cob = checkout -b
  cm = commit -m
  di = diff
  purr = pull --rebase
  who = shortlog -n -s --no-merges
  lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

The first few just save keystrokes, but those last two are a bit fancier. I learned about git lg over here, and it's much nicer than a standard git log. I didn't even know about git shortlog until I discovered the git who alias, thanks to thoughtbot.

Personally, I find these aliases useful, but don't be afraid to write your own. Take the things I've taught you and build a deep relationship with git that stands the test of time.