Git: How to enable autocorrect

I think you meant Qe5!

Update (2023-10-16): An improved version of this post’s content appears in my new book Boost Your Git DX.

By default, if you mistype a Git command, it will list similar commands that you might have meant:

$ git comit -m "Some awesome work"
git: 'comit' is not a git command. See 'git --help'.

The most similar command is
  commit

You can configure Git to actually run the first match automatically. This saves you a few seconds each time you make a typo.

To enable autocorrect, set the help.autocorrect:

$ git config --global help.autocorrect immediate

This will add to your ~/.gitconfig file:

[help]
    autocorrect = immediate

With this in place, Git will automatically correct typo’d commands instantly:

$ git comit -m "Some awesome work"
WARNING: You called a Git command named 'comit', which does not exist.
Continuing under the assumption that you meant 'commit'.
[main 25112f085] Some awesome work

Neat.

Git only automatically proceeds if there’s only one command that’s a significantly close match. If there are several potential matches, it still lists them and stops:

$ git com -m "Some awesome work"
git: 'com' is not a git command. See 'git --help'.

The most similar commands are
  commit
  column

Safe.

Prompt to run

If automatically proceeding to run the closest matched command makes you a little nervous, you can instead use the “prompt” mode:

$ git config --global help.autocorrect prompt

Typos will then show a yes/no prompt to correct:

$ git comit -m "More awesome work"
WARNING: You called a Git command named 'comit', which does not exist.
Run 'commit' instead [y/N]?

Press “y” then “enter” to accept the correction:

$ git comit -m "More awesome work"
WARNING: You called a Git command named 'comit', which does not exist.
Run 'commit' instead [y/N]? y
[main 2183b892e] More awesome work

Easy does it.

With a timeout

There’s a third mode, a kind of middle ground between “immediate” and “prompt”. With a numerical setting, Git will wait that many deciseconds before automatically continuing. This gives you a small window of time to stop the corrected command if you spot that it is not what you intended.

For example, to use a two second delay:

$ git config --global help.autocorrect 20

Typo’d commands then show “Continuning in 2.0 seconds...”:

$ git comit -m "Even more work" --allow-empty
WARNING: You called a Git command named 'comit', which does not exist.
Continuing in 2.0 seconds, assuming that you meant 'commit'.
[main b0be18f0d] Even more work

You can press Ctrl-C to cancel incorrect commands during the pause.

Personally I prefer immediate mode—it’s normally possible to recover from erroneous commands.

Fin

May Git’s aucotorrect sreve you well,

—Adam


Read my book Boost Your Git DX for many more Git lessons.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: