Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make git do the "did you mean" suggestion?

Tags:

git

People also ask

How do you help for any of the Git commands?

Git Help can be accessed from your Git Bash just by typing the command git help . Press Enter to execute the help command. This command will display all the information about Git. These are brief information to get any beginner started.

Why do we use Git commands?

Git is a distributed version-control system for tracking changes in any set of files, originally designed for coordinating work among programmers cooperating on source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows (Source: Wikipedia).


According to git-config(1), you want to set help.autocorrect appropriately. For example, git config --global help.autocorrect 5 will make it wait half a second before running the command so you can see the message first.


The autocorrect is nice, but my OCD-self needs a little more control over what's going on. So, I wrote a straightforward script that just chooses the first suggestion provided by git. You run the script after the failed command and use the built in bash history substitution "bang bang" syntax. Also, if you are typing something that could possibly have more than one command, this command lets you choose one other than the first option.

It would look something like this,

kristian@office:~/myrepo$ git puhs
git: 'puhs' is not a git command. See 'git --help'

Did you mean this?
      push

kristian@office:~/myrepo$ idid !!
Counting objects: 18, done.
Delta compression using up to 32 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 1.17 KiB, done.
Total 10 (delta 6), reused 0 (delta 0)

Plus, it's fun to type anything with two exclamation points. So bonus for that.

Here's a gist with my script


Also take a look at thefuck

It can correct typos, and also perform suggestions. Not just limited to git.


As an alternative to help.autocorrect: if you make the same typos all the time, you can create aliases for them in your .gitconfig file

[alias]
    puhs = push

(I do this with shell aliases too, where I can never seem to type mkae^H^H^H^Hmake correctly.)


Is there a way to make git prompt before correcting, similar to zsh?
(e.g., "correct 'puhs' to 'push' [yn]?")

That was asked in 2015. 6 years later:

With Git 2.34 (Q4 2021), the logic for auto-correction of misspelt subcommands learned to go interactive when the help.autocorrect configuration variable is set to 'prompt'.

See commit dc66e3c (14 Aug 2021) by Azeem Bande-Ali (azeemba).
(Merged by Junio C Hamano -- gitster -- in commit 96ac07f, 10 Sep 2021)

help.c: help.autocorrect=prompt waits for user action

Signed-off-by: Azeem Bande-Ali

If help.autocorrect is set to 'prompt', the user is prompted before the suggested action is executed.

Based on original patch by David Barr (from... Sept. 2010!).

git config now includes in its man page:

git will try to suggest the correct command or even run the suggestion automatically.

Possible config values are:

  • 0 (default): show the suggested command.
  • positive number: run the suggested command after specified deciseconds (0.1 sec).
  • "immediate": run the suggested command immediately.
  • "prompt": show the suggestion and prompt for confirmation to run the command.
  • "never": don't run or show any suggested command.