在Windows命令提示符下获取git帮助

19
在 Windows 上(使用 msysgit 发行版),每次运行“git help”命令都会产生一个 Web 浏览器。我尝试了“git help -m”,它报告“无手册条目...”,以及“git help -i”,它说“信息:终端类型 'msys' 不够聪明,无法运行 Info。”在 Cygwin 下的 bash 中也是如此。是否有任何明智的方法在 cmd 终端中获得轻量级的帮助?

你可以使用MSYS2安装git和man,命令是pacman -S git man。安装完成后,你可以运行依赖于man的命令,例如git init --help。将C:\msys64\usr\bin添加到PATH中后,使用powershellcmd就像内置命令一样更加方便。 - cpprust
4个回答

13

它适用于特定的命令:git <command> -h

编辑,感谢@the-happy-hippo的提醒

但是它只显示简要描述,不像Windows上的git help <command>git <command> --help那样显示完整说明。


是的,这将显示简短的帮助信息,但不包括完整的手册页面、扩展描述和示例等内容(例如,将 git help add -m 的输出与 git help add 的输出进行比较)。 - the-happy-hippo
编辑过了,感谢澄清。我对你的示例不是很确定,前者试图给人提供帮助,但由于我的Windows上没有man而失败(参见https://dev59.com/_W035IYBdhLWcg3wSN0G#5517857),后者只提供html帮助... - tsul
2
知道使用-h可以获得简短的帮助信息是很有用的。 - Russell Gallop

8

git <verb> -h 显示相同终端窗口中的命令用法。

另一方面,git <verb> --helpgit help <verb> 打开浏览器。


2

Git 2.x更新(2017年6月,Git 2.13.1)

你仍然没有man:

> git -c help.format=man help add
warning: failed to exec 'man': No such file or directory
fatal: no man viewer handled the request

对于git <verb> --help同样适用。
git <verb> -h不会打印man页面,只会打印简短的使用部分(与man无关)


在 Git 2.34(2021 年第四季度)中,当 git cmd -h 显示多行用法文本时(例如 cmd 子命令可能需要子子命令),parse-options API 学会了对齐这些行,甚至跨越 i18n/l10n

请查看 提交 4631cfc(2021年9月21日),以及 提交 84122ec提交 78a5091提交 5d70198(2021年9月13日),作者为 Ævar Arnfjörð Bjarmason (avar)
(由 Junio C Hamano -- gitster -- 合并于 提交 d7bc852,2021年10月13日)

parse-options:正确对齐持续使用输出 签署者:Ævar Arnfjörð Bjarmason

Some commands such as "git stash"(man) emit continued options output with e.g. git stash -h, because usage_with_options_internal() prefixes with its own whitespace the resulting output wasn't properly aligned.
Let's account for the added whitespace, which properly aligns the output.

The "git stash" command has usage output with a N_() translation that legitimately stretches across multiple lines;

N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
   "          [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
[...]

We'd like to have that output aligned with the length of the initial "git stash" output, but since usage_with_options_internal() adds its own whitespace prefixing we fell short, before this change we'd emit:

$ git stash -h
usage: git stash list [<options>]
   or: git stash show [<options>] [<stash>]
   [...]
   or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
          [-u|--include-untracked] [-a|--all] [-m|--message <message>]
          [...]

Now we'll properly emit aligned output.
I.e.
the last four lines above will instead be (a whitespace-only change to the above):

[...]
or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
              [-u|--include-untracked] [-a|--all] [-m|--message <message>]
              [...]

This change is relatively more complex since I've accounted for making it future-proof for RTL translation support.
Later in usage_with_options_internal() we have some existing padding code dating back to d7a38c5 ("parse-options: be able to generate usages automatically", 2007-10-15, Git v1.5.4-rc0 -- merge) which isn't RTL-safe, but that code would be easy to fix.
Let's not introduce new RTL translation problems here.


原始答案(2014年)

不,即使在“{{link1:如何在Windows中获取git显示命令行帮助?}}”中建议基于htlp txt文件的“猫”的替代方案。

2008年引入了{{link2:man.<tool>.cmd config}},允许设置自定义命令,但msys shell未随附man.exe


不仅 man.exe 缺失,Git 2.13.1 中的 man 页面也缺失了。 - tsul

-1

适用于此问题的世界上最过度工程化的变通方法:使用WSL

(也就是说,除非您已经是WSL用户,否则它只是一种普通的解决方法)

  1. 通过Windows商店安装其中一个Linux发行版
  2. 进入并确保其已安装git软件包
  3. 从Windows命令行中运行:bash -c 'git help fetch'

下面是最后一个命令的别名:

[alias]
    hep = "!f() { $SYSTEMROOT/System32/bash -c \"git help $1\"; }; f"

(不,你不能覆盖git内置函数, 但是你可以创建一个shell命令来拦截和重定向help。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接