published on in git bash

Autocorrecting my Git Commands

⬅️ Previous post: PrettyGoodPing: Now with Domain Name Expiration

❤️ It's great to see you here! I'm currently available on evenings and weekends for consulting and freelance work. Let's chat about how I can help you achieve your goals.



I spend most of my day at the command line and, although I took a couple of years of typing classes in high school, typos are constantly tripping me up.

calendar

"Typewriter (pre-email relict)" by mripp is licensed under CC BY 2.0 .

With a lot of my time on the command line being spent using git, I like to take advantage of git’s ability to fix typos automatically. In my dot files I run this command as part of my git configuration:

git config --global help.autocorrect 10

This command enables help.autocorrect in your global git config. The --global flag means that the value you set will end up in your ~/.gitconfig and for this reason will be a default for all of your git repositories on this machine. You can always override the global config locally for any repository for which you don’t want this enabled. If you don’t want this enabled globally, just do it inside the repo where you want it enabled and omit the --global flag:

git config help.autocorrect 10

help.autocorrect is pretty much what it sounds like. You’re giving git permission to try to make sense of your mangled command line input. Note that git’s behaviour here is more about asking forgiveness than permission. It tells you that it has rewritten your command and gives you an announced time to make it stop.

The 10 in the configuration means that git will give you exactly 1 second to ctrl-c and bail on its suggested correction before it goes ahead and does it. So, it’s an integer which represents 10ths of a second. Set it to 100 if you want to wait 10 seconds etc. (I believe I initially used a larger number until I was comfortable that this setting was not going to get me in trouble). You may want to start with a less aggressive value.

Now, let’s see how this works:

git poush
WARNING: You called a Git command named 'poush', which does not exist.
Continuing in 1.0 seconds, assuming that you meant 'push'.

That’s pretty basic. git knows that poush is not a command, but push is, so it announces its intention to “Do What I Mean” and gives me 1.0 seconds to intervene. That in itself makes for a smoother day for me. We can take it one step further.

In my ~/.bashrc I’ve added the following lines:

alias gi=git  # fix typos
alias gti=git # fix typos

This creates bash aliases which mean that typing gi or gti will invoke git rather than saying that the command is not found. That means I can happily (mis)type something like gi tpush and git can save me from myself. Let’s see it in action:

gi tpush
WARNING: You called a Git command named 'tpush', which does not exist.
Continuing in 1.0 seconds, assuming that you meant 'push'.

As a caveat emptor, I should add that things could go sometimes go in an unplanned direction.

gi tpoush
WARNING: You called a Git command named 'tpoush', which does not exist.
Continuing in 1.0 seconds, assuming that you meant 'http-push'.
usage: git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]

I can’t recall that this has ever bitten me in a bad way, but it’s something to be aware of.

That’s it! You may think this is a terrible idea and you may be right. Personally I’ve been doing this for many years now and it’s a genuine pleasure when one of my botched commands ends up doing exactly what I intended.