little cubes

A Shortcut For Git (on Windows)

Zach Olivare - 2017 Feb 20

What are the three most common letters you type when using git? 'g-i-t'. What if you could reduce that by two thirds?

Efficiency

Efficiency is a topic that I believe very strongly in. Why do more work than you have to in order to accomplish a given task? Why perform a task over and over by hand, when it can be automated? I'm planning to write a series of posts about Git, and I wanted to start off the collection with something short, sweet, and extremely useful.

Alias

When using Git (the CLI of course, don't get me started on Git GUIs), what are the three letters that you type the most frequently? Well "git" of course, as it's the prefix to every command. This seems wasteful doesn't it? I probably run 200-500 Git commands per typical day of work; a potential 1500 characters. I can type somewhere around 500 characters per minute, so that could be as much as 3 minutes of my day doing nothing other than typing "git".

Well what's the best case? I need some way of identifying that I want to run a Git command, and not just a terminal command. Optimally, I could type one letter, and then a space, and then the rest of my Git instruction. Well I've got good news, as you probably already figured out, that's what this post is supposed to show you! We're going to write a special kind of alias to map git to k.

Choosing a letter

You can choose any letter. You have exactly 26 choices. You might even be able to use special characters, but I haven't tried that, and you certainly couldn't use all of them. After a little thought though, I chose k. I tried to figure out what the easiest key to type on the keyboard was. It needed to of course be on the home row, and with the middle finger being the longest, it seems easiest to press a key with that finger. So that leaves d and k, but that selection is somewhat arbitrary, and as I said it's completely up to you!

Making the change

The first thing you need to do is open your .bashrc file. For Windows users, this is located in your home directory, ~/.bashrc, or C:\Users\<USERNAME>\.bashrc. If that file doesn't exist, go ahead and make it!

Then paste this text into it:

#!/bin/bash # Where 'k' is your shortcut letter alias k='git'

Well that was easy! Open a bash shell and try it out! You'll have to close and re-open any bash windows that you currently have open. It's worth noting that this alias will not work in the windows command prompt.

One final thing, after using that shortcut for a little while, you might notice that sadly, the handy Git tab-complete no longer works. But you can re-enable it by adding one more line to your .bashrc:

# Where 'k' is your shortcut letter complete -o default -o nospace -F _git k

Putting everything together, you have:

#!/bin/bash alias k='git' complete -o default -o nospace -F _git k