如何关闭 git 自动更正?

How do I turn off git autocorrect?

当我输入 git 命令时,例如输入 git git checkout myfile(注意多余的 "git"),我得到以下输出:

WARNING: You called a Git command named 'git', which does not exist.
Continuing under the assumption that you meant 'init'
in 0.1 seconds automatically...
usage: git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]

所以 git 假定我指的是 init,并给我所有 0.1 秒的时间来查看错误,然后再继续。不酷git!

如何关闭此 "feature"?

要查看自动更正设置,请键入:

git config help.autocorrect

根据 the docs:

help.autocorrect实际上是一个整数,表示十分之一秒。因此,如果您将其设置为 50,Git 将在执行自动更正命令之前给您 5 秒的时间改变主意。

要关闭此功能,请使用命令:

git config --global help.autocorrect 0

请注意 help.autocorrect 的默认值为 0。

您也可以将其设置为大于 1 的值:它使用分秒,因此 10 为 1 秒,30 为 3 秒,依此类推。

请注意,从 Git 2.14 开始,警告和继续消息将发生变化。
参见 commit 968b1fe (21 Jun 2017) by Marc Branchaud (``)
(由 Junio C Hamano -- gitster -- in commit aca226e 合并,2017 年 6 月 26 日)

之前,help.autoCorrect = 15

WARNING: You called a Git command named 'lgo', which does not exist.
   Continuing under the assumption that you meant 'log'
   in 1.5 seconds automatically...

之后:

WARNING: You called a Git command named 'lgo', which does not exist.
Continuing in 1.5 seconds, assuming that you meant 'log'.

FWIW,您也可以使用 -c help.autocorrect=0 作为任何 Git 命令的参数。例如:

git -c help.autocorrect=0 svn find-rev 5e2272613fa

它对脚本很有用,因为您不会修改您 运行 所在的环境。

在这种情况下,除非安装了 git-svn,否则 Git 将无法识别 svn 命令并尝试退回到 serve,这在脚本中可能是灾难性的。

关闭它的最新方法(2020 年)是 Git 2.30(2021 年第一季度):“git $cmd $args,当 $cmdnot 一个可识别的子命令,默认情况下会尝试查看 $cmd 是否是现有子命令的拼写错误,如果只有一种可能性,则可以选择执行更正的命令,具体取决于 help.autocorrect.

用户现在可以通过将配置变量设置为 '[=14= 来禁用整个东西,包括用于查找可能的拼写错误的周期]'.

参见 commit 644bb95 (25 Nov 2020) by Drew DeVault (ddevault)
(由 Junio C Hamano -- gitster -- in commit 78abcff 合并,2020 年 12 月 14 日)

help.c: help.autocorrect=never means "do not compute suggestions"

Signed-off-by: Drew DeVault

While help.autocorrect can be set to 0 to decline auto-execution of possibly mistyped commands, it still spends cycles to compute the suggestions, and it wastes screen real estate.

Update help.autocorrect to accept the string "never" to just exit with error upon mistyped commands to help users who prefer to never see suggested corrections at all.

While at it, introduce "immediate" as a more readable way to immediately execute the auto-corrected command, which can be done with negative value.

git config 现在包含在其 man page 中:

If git detects typos and can identify exactly one valid command similar to the error, git will automatically run the intended command after waiting a duration of time defined by this configuration value in deciseconds (0.1 sec).

  • If this value is 0, the suggested corrections will be shown, but not executed.
  • If it is a negative integer, or "immediate", the suggested command is run immediately.
  • If "never", suggestions are not shown at all.

The default value is zero.