English ▾ (英语 ▾) 主题 ▾ 最新版本 ▾ 编码规范最后更新于 2.49.0

此信息特定于 Git 项目

请注意,如果您计划为 Git 项目本身做贡献,此信息才与您相关。 对于普通 Git 用户来说,它绝对不是必读内容。

与其他项目一样,我们也有一些代码准则。 对于一般的 Git,一些粗略的规则是

  • 最重要的是,我们从不说“它在 POSIX 中;如果您的系统不符合 POSIX,我们会很乐意忽略您的需求。” 我们生活在现实世界中。

  • 但是,我们经常说“让我们远离这种结构,它甚至不在 POSIX 中”。

  • 尽管有上述两个规则,但我们有时会说“尽管这不在 POSIX 中,但它(非常方便 | 使代码更具可读性 | 具有其他良好特性),并且实际上我们关心的所有平台都支持它,所以让我们使用它”。

    Again, we live in the real world, and it is sometimes a
    judgement call, the decision based more on real world
    constraints people face than what the paper standard says.
  • 在处理实际更改时修复样式违规行为作为准备性的清理步骤是好的,但除此之外,避免为了符合样式而进行无用的代码变更。

    "Once it _is_ in the tree, it's not really worth the patch noise to
    go and fix it up."
    Cf. https://lore.kernel.org/all/20100126160632.3bdbe172.akpm@linux-foundation.org/
  • 解释您的更改的日志消息与更改本身一样重要。 清晰编写的代码和代码内注释解释了代码的工作方式以及从周围环境中假定了什么。 日志消息解释了更改想要实现的目标以及为什么需要进行更改(更多信息请参见随附的 SubmittingPatches 文档)。

使您的代码可读且合理,不要试图变得聪明。

至于更具体的准则,只需模仿现有代码即可(无论您为哪个项目做贡献,这都是一个好的准则)。 最好始终匹配本地约定。 添加到 Git 套件的新代码应与现有代码的总体样式相匹配。 对现有代码的修改应与周围代码已使用的样式相匹配(即使它与现有代码的总体样式不匹配)。

但是,如果您必须有一系列规则,则以下是一些特定于语言的规则。 请注意,Documentation/ToolsForGit.adoc 文档收集了一些技巧,可帮助您使用一些外部工具来符合这些准则。

对于特定的 shell 脚本(并非详尽无遗)

  • 我们使用制表符进行缩进。

  • Case 分支臂的缩进深度与 case 和 esac 行相同,如下所示

    case "$variable" in
    pattern1)
    	do this
    	;;
    pattern2)
    	do that
    	;;
    esac
  • 重定向运算符应在前面写一个空格,但在后面不写空格。 换句话说,编写 echo test >"$file" 而不是 echo test> $fileecho test > $file。 请注意,即使 POSIX 没有要求对变量中的重定向目标使用双引号(如上所示),我们的代码也会这样做,因为某些版本的 bash 在没有引号的情况下会发出警告。

    (incorrect)
    cat hello > world < universe
    echo hello >$world
    (correct)
    cat hello >world <universe
    echo hello >"$world"
  • 我们更喜欢使用 $( …​ ) 进行命令替换; 与 `` 不同,它可以正确嵌套。 它本应是 Bourne 从一开始就拼写它的方式,但不幸的是不是。

  • 如果您想知道用户的 $PATH 上是否有可用的命令,则应使用 type <command>,而不是 which <command>which 的输出不可由机器解析,并且其退出代码在各个平台上不可靠。

  • 我们使用符合 POSIX 标准的参数替换,并避免使用 bashisms; 即

  • 我们使用 ${parameter-word} 及其 [-=?+] 同级,以及它们的冒号形式“未设置或为空”。

  • 我们使用 ${parameter#word} 及其 [#%] 同级,以及它们的双重“最长匹配”形式。

  • 没有“子字符串扩展”${parameter:offset:length}。

  • 没有 shell 数组。

  • 没有模式替换 ${parameter/pattern/string}。

  • 我们使用算术扩展 $…​。

  • 我们不使用进程替换 <(list) 或 >(list)。

  • 不要用分号在一行上编写控制结构。“then”应该在 if 语句的下一行上,“do”应该在“while”和“for”的下一行上。

    (incorrect)
    if test -f hello; then
    	do this
    fi
    (correct)
    if test -f hello
    then
    	do this
    fi
  • 如果用 && 或 || 或 | 连接的命令序列跨越多行,则将每个命令放在单独的行上,并将 && 和 || 和 | 运算符放在每行的末尾,而不是开头。 这意味着您不需要使用 \ 来连接行,因为上述运算符意味着序列尚未完成。

    (incorrect)
    grep blob verify_pack_result \
    | awk -f print_1.awk \
    | sort >actual &&
    ...
    (correct)
    grep blob verify_pack_result |
    awk -f print_1.awk |
    sort >actual &&
    ...
  • 我们更喜欢使用“test”而不是“[ …​ ]”。

  • 我们不在 shell 函数前面写噪音词“function”。

  • 我们更喜欢在函数名称和括号之间添加空格,并且括号内没有空格。 开头的“{”也应该在同一行上。

    (incorrect)
    my_function(){
    	...
    (correct)
    my_function () {
    	...
  • 至于 grep 的使用,坚持使用 BRE 的子集(即,没有 \{m,n\}、[::]、[==] 或 [..])以提高可移植性。

  • 我们不使用 \{m,n\};

  • 我们不使用 ? 或 +(它们在 BRE 中分别为 \{0,1\} 和 \{1,\}),但这不用说,因为它们是 ERE 元素而不是 BRE(请注意,\? 和 \+ 甚至不是 BRE 的一部分 — 使它们可以从 BRE 访问是 GNU 扩展)。

  • 使用 git-sh-i18n 中的 Git 的 gettext 包装器使用户界面可翻译。 请参阅 po/README 中的“标记字符串以进行翻译”。

  • 我们不使用带有“-a”和“-o”的“test”命令,而是使用“&&”或“||”来连接多个“test”命令,因为使用“-a/-o”通常容易出错。 例如

    test -n "$x" -a "$a" = "$b"
    is buggy and breaks when $x is "=", but
    test -n "$x" && test "$a" = "$b"
    does not have such a problem.
  • 即使“local”不是 POSIX 的一部分,我们也在测试套件中大量使用它。 我们不在脚本化的 Porcelains 中使用它,并且希望在所有重要的 shell 都支持它之前没有人开始使用“local”(值得注意的是,来自 AT&T Research 的 ksh 尚不支持它)。

  • 某些版本的 shell 不理解“export variable=value”,因此我们将“variable=value”写在一行上,然后在另一行上写“export variable”。

  • 某些版本的 dash 在以“local”、“export”和“readonly”为前缀的变量赋值时会中断,除非引用,否则要分配的值会在 $IFS 处进行字段拆分。

    (incorrect)
    local variable=$value
    local variable=$(command args)
    (correct)
    local variable="$value"
    local variable="$(command args)"
  • 常见的结构

    VAR=VAL command args
    to temporarily set and export environment variable VAR only while
    "command args" is running is handy, but this triggers an
    unspecified behaviour according to POSIX when used for a command
    that is not an external command (like shell functions).  Indeed,
    dash 0.5.10.2-6 on Ubuntu 20.04, /bin/sh on FreeBSD 13, and AT&T
    ksh all make a temporary assignment without exporting the variable,
    in such a case.  As it does not work portably across shells, do not
    use this syntax for shell functions.  A common workaround is to do
    an explicit export in a subshell, like so:
    (incorrect)
    VAR=VAL func args
    (correct)
    (
    	VAR=VAL &&
    	export VAR &&
    	func args
    )
    but be careful that the effect "func" makes to the variables in the
    current shell will be lost across the subshell boundary.
  • 在 printf 格式字符串中使用八进制转义序列(例如“\302\242”),而不是十六进制(例如“\xc2\xa2”),因为十六进制转义序列不可移植。

对于 C 程序

  • 我们使用制表符进行缩进,并将制表符解释为最多占用 8 个空格。

  • 嵌套的 C 预处理器指令在哈希后缩进,每级嵌套一个空格。

    #if FOO
    # include <foo.h>
    # if BAR
    #  include <bar.h>
    # endif
    #endif
  • 我们尽量保持每行最多 80 个字符。

  • 作为 Git 开发人员,我们假设您有一个相当现代的编译器,我们建议您启用 DEVELOPER makefile 开关,以确保您的补丁没有我们关心的所有编译器警告,例如,“echo DEVELOPER=1 >>config.mak”。

  • 使用 DEVELOPER=1 模式时,您可能会看到来自编译器的警告,例如“error: unused parameter foo [-Werror=unused-parameter]”,这表明函数忽略了它的参数。 如果无法删除未使用的参数(例如,因为该函数用作回调并且必须匹配某个接口),您可以注释单个参数与UNUSED(或MAYBE_UNUSED)关键字,像“int foo UNUSED”。

  • 我们尝试支持各种 C 编译器来编译 Git,包括旧的编译器。 截至 Git v2.35.0,Git 需要 C99(我们检查“STDC_VERSION”)。 您不应使用更新的 C 标准中的功能,即使您的编译器可以理解它们。

    New C99 features have been phased in gradually, if something's new
    in C99 but not used yet don't assume that it's safe to use, some
    compilers we target have only partial support for it. These are
    considered safe to use:
    1. 自 2007 年左右的 2b6854c863a 以来,我们一直在使用在加载时不可计算的初始化程序元素。 例如

      const char *args[] = { "constant", variable, NULL };
    2. 自 2012 年初的 e1327023ea 以来,我们一直在使用枚举定义,其最后一个元素后跟一个逗号。 就像以尾随逗号结尾的数组初始化程序一样,这可以用于减少在末尾添加新标识符时的补丁噪音。

    3. 自 2017 年中期的 cbc0f81d 以来,我们一直在使用结构的指定初始化器(例如“struct t v = { .val = a };”)。

    4. 自 2017 年中期的 512f41cf 以来,我们一直在使用数组的指定初始化器(例如“int array[10] = { [5] = 2 }”)。

    5. 自 2021 年初的 765dc168882 以来,我们一直在使用可变参数宏,主要用于类似 printf 的跟踪和调试宏。

    6. 自 2021 年末的 44ba10d6 以来,我们已经在 for 循环“for (int i = 0; i < 10; i++)”中声明了变量。

      New C99 features that we cannot use yet:
    7. %z 和 %zu 作为 size_t 的 printf() 参数(%z 用于特定于 POSIX 的 ssize_t)。 相反,您应该使用 printf("%"PRIuMAX, (uintmax_t)v)。 现在我们依赖的 MSVC 版本支持 %z,但 MinGW 使用的 C 库不支持。

    8. 在结构初始化中使用 ".a.b = *c" 等简写方式已知会绊倒较旧的 IBM XLC 版本,请改用 ".a = { .b = *c }"。 请参阅 33665d98(reftable:使赋值可移植到 AIX xlc v12.01,2022-03-28)。

  • 变量必须在代码块的开头声明,在第一个语句之前(例如 -Wdeclaration-after-statement)。 建议在声明结束和代码块中的第一个语句之间留一个空行。

  • NULL 指针应写为 NULL,而不是 0。

  • 当声明指针时,星号应该靠近变量名,例如 "char string",而不是 "char string" 或 "char * string"。 这样更容易理解像 "char *string, c;" 这样的代码。

  • 在运算符和关键字周围使用空格,但在括号内和函数周围不要使用空格。 所以

          while (condition)
    func(bar + 1);
    and not:
          while( condition )
    func (bar+1);
  • 二元运算符(除了“,”)和三元条件运算符“?:”的操作符两侧都有空格,以便将其与其操作数分开。 例如:“A + 1”,而不是“A+1”。

  • 一元运算符(除了“.”和“→”)在其和操作数之间没有空格。 例如:“(char *)ptr”,而不是“(char *) ptr”。

  • 不要将整数值与常量 0 或 \0 显式比较,也不要将指针值与常量 NULL 显式比较。 例如,要验证计数数组 <ptr, cnt> 已初始化但没有元素,请编写

    if (!ptr || cnt)
    	BUG("empty array expected");
    and not:
    if (ptr == NULL || cnt != 0);
    	BUG("empty array expected");
  • 我们避免不必要地使用花括号。 即:

    if (bla) {
    	x = 1;
    }
    is frowned upon. But there are a few exceptions:
  • 当语句扩展到几行时(例如,带有嵌入式条件的 while 循环或注释)。 例如:

    while (foo) {
    	if (x)
    		one();
    	else
    		two();
    }
    if (foo) {
    	/*
    	 * This one requires some explanation,
    	 * so we're better off with braces to make
    	 * it obvious that the indentation is correct.
    	 */
    	doit();
    }
  • 当条件语句有多个分支,并且其中一些分支需要花括号时,为了保持一致性,即使是单行代码块也用花括号括起来。 例如:

    if (foo) {
    	doit();
    } else {
    	one();
    	two();
    	three();
    }
  • 我们尽量避免在“if”语句的条件中赋值。

  • 尽量让你的代码易于理解。 你可以添加注释,但是当代码发生变化时,注释总是趋于过时。 通常将一个函数分成两个函数会使代码的意图更加清晰。

  • 多行注释的开始和结束分隔符与文本内容分开放置在单独的行上。 例如:

    /*
     * A very long
     * multi-line comment.
     */
    Note however that a comment that explains a translatable string to
    translators uses a convention of starting with a magic token
    "TRANSLATORS: ", e.g.
    /*
     * TRANSLATORS: here is a comment that explains the string to
     * be translated, that follows immediately after it.
     */
    _("Here is a translatable string explained by the above.");
  • 双重否定通常比没有否定更难理解。

  • 在比较方面有两种思路,尤其是在循环中。 有些人喜欢将不太稳定的值放在左侧,而将更稳定的值放在右侧,例如,如果你有一个循环将变量 i 递减到下限,

    while (i > lower_bound) {
    	do something;
    	i--;
    }
    Other people prefer to have the textual order of values match the
    actual order of values in their comparison, so that they can
    mentally draw a number line from left to right and place these
    values in order, i.e.
    while (lower_bound < i) {
    	do something;
    	i--;
    }
    Both are valid, and we use both.  However, the more "stable" the
    stable side becomes, the more we tend to prefer the former
    (comparison with a constant, "i > 0", is an extreme example).
    Just do not mix styles in the same part of the code and mimic
    existing styles in the neighbourhood.
  • 在将长逻辑行拆分为多行时,有两种思路。 有些人使用制表符将第二行和后续行向右推足够远并对齐它们

          if (the_beginning_of_a_very_long_expression_that_has_to ||
    span_more_than_a_single_line_of ||
    the_source_text) {
                  ...
    while other people prefer to align the second and the subsequent
    lines with the column immediately inside the opening parenthesis,
    with tabs and spaces, following our "tabstop is always a multiple
    of 8" convention:
       if (the_beginning_of_a_very_long_expression_that_has_to ||
    span_more_than_a_single_line_of ||
    the_source_text) {
               ...
    Both are valid, and we use both.  Again, just do not mix styles in
    the same part of the code and mimic existing styles in the
    neighbourhood.
  • 在拆分一个长逻辑行时,有些人会在二元运算符之前更改行,这样当你逆时针旋转 90 度时,结果看起来像一个解析树

       if (the_beginning_of_a_very_long_expression_that_has_to
    || span_more_than_a_single_line_of_the_source_text) {
    while other people prefer to leave the operator at the end of the
    line:
       if (the_beginning_of_a_very_long_expression_that_has_to ||
    span_more_than_a_single_line_of_the_source_text) {
    Both are valid, but we tend to use the latter more, unless the
    expression gets fairly complex, in which case the former tends to
    be easier to read.  Again, just do not mix styles in the same part
    of the code and mimic existing styles in the neighbourhood.
  • 在拆分一个长逻辑行时,在所有其他条件相同的情况下,最好在解析树中较高级别的运算符之后拆分。 也就是说,这更可取

    if (a_very_long_variable * that_is_used_in +
        a_very_long_expression) {
    	...
    than
    if (a_very_long_variable *
        that_is_used_in + a_very_long_expression) {
    	...
  • 一些巧妙的技巧,比如将 !! 运算符与算术结构一起使用,可能会让其他人非常困惑。 避免使用它们,除非有令人信服的理由。

  • 使用 API。 真的。 我们有一个 strbuf(可变长度字符串)、几个带有 ALLOC_GROW() 宏的数组、一个用于排序字符串列表的 string_list、一个名为“struct decorate”的哈希映射(映射结构对象),以及其他东西。

  • 当你提出一个 API 时,在将 API 公开给调用者的头文件中记录其函数和结构。 以“strbuf.h”中的内容作为适当的语气和详细程度的模型。

  • C 文件中的第一个 #include,除了特定于平台的 compat/ 实现和 sha1dc/ 中的 #include,必须是 <git-compat-util.h>。 此头文件将其他头文件和源文件与平台差异隔离开来,例如哪些系统头文件必须以什么顺序包含,以及必须定义哪些 C 预处理器功能宏来触发我们期望系统提供的某些功能。 由此可以推论,C 文件不应直接包含系统头文件本身。

    There are some exceptions, because certain group of files that
    implement an API all have to include the same header file that
    defines the API and it is convenient to include <git-compat-util.h>
    there.  Namely:
  • 内置命令在“builtin/”目录中的实现,这些实现包含“builtin.h”以获取 cmd_foo() 原型定义,

  • “t/helper/”目录中的测试助手程序,这些程序包含“t/helper/test-tool.h”以获取 cmd__foo() 原型定义,

  • “xdiff/”目录中的 xdiff 实现,该实现包含“xdiff/xinclude.h”以获取 xdiff 机制内部组件,

  • “t/unit-tests/”目录中的单元测试程序,这些程序包含“t/unit-tests/test-lib.h”,该文件为它们提供单元测试框架,以及

  • 在“reftable/”目录中实现 reftable 的源文件,这些文件包含“reftable/system.h”以获取 reftable 内部组件,

    are allowed to assume that they do not have to include
    <git-compat-util.h> themselves, as it is included as the first
    '#include' in these header files.  These headers must be the first
    header file to be "#include"d in them, though.
  • C 文件必须直接包含声明其使用的函数和类型的头文件,除非这些函数和类型通过包含其必须通过先前规则包含的头文件之一来提供。

  • 如果您正在计划一个新的命令,请考虑首先用 shell 或 perl 编写它,以便可以轻松更改和讨论语义。 许多 Git 命令都是这样开始的,并且仍然有一些是脚本。

  • 避免向 Git 引入新的依赖项。 这意味着通常你应该远离 Git 核心命令集中尚未使用的脚本语言(除非你的命令明显与它分开,例如将随机scm-X 存储库转换为 Git 的导入器)。

  • 当我们向函数传递 <string, length> 对时,我们应该尽量按照该顺序传递它们。

  • 使用 Git 的 gettext 包装器使用户界面可翻译。 请参阅 po/README 中的“标记字符串以进行翻译”。

  • 给定源文件的局部变量和函数应标记为“static”。 其他源文件可见的变量必须在头文件中使用“extern”声明。 但是,函数声明不应使用“extern”,因为这已经是默认设置。

  • 您可以使用简写 GIT_DEBUGGER 在程序周围启动 gdb。 运行 GIT_DEBUGGER=1 ./bin-wrappers/git foo 以简单地按原样使用 gdb,或运行 GIT_DEBUGGER="<debugger> <debugger-args>" ./bin-wrappers/git foo 以使用您自己的调试器和参数。 示例:GIT_DEBUGGER="ddd --gdb" ./bin-wrappers/git log(参见 bin-wrappers/wrap-for-bin.sh。)

  • 子系统 *S* 处理的主要数据结构称为 struct S。 对 struct S 进行操作的函数命名为 S_<verb>(),通常应接收指向 struct S 的指针作为第一个参数。 例如:

    struct strbuf;
    void strbuf_add(struct strbuf *buf, ...);
    void strbuf_reset(struct strbuf *buf);
    is preferred over:
    struct strbuf;
    void add_string(struct strbuf *buf, ...);
    void reset_strbuf(struct strbuf *buf);
  • 有一些常见的习惯用法名称用于对结构体 S 执行特定任务的函数

  • S_init() 初始化一个结构体,而不分配结构体本身。

  • S_release() 释放结构体的内容而不释放结构体。

  • S_clear() 相当于 S_release() 之后紧接着 S_init(),这样结构体在清除后可以直接使用。 当提供 S_clear() 时,S_init() 不应分配需要再次释放的资源。

  • S_free() 释放结构体的内容并释放结构体。

  • 函数名称应清晰且具有描述性,准确反映其目的或行为。 不添加有意义上下文的任意后缀可能会导致混淆,特别是对于代码库的新手。

    Historically, the '_1' suffix has been used in situations where:
  • 函数处理一组需要类似处理的元素中的一个元素。

  • 递归函数已与其设置阶段分离。

    The '_1' suffix can be used as a concise way to indicate these specific
    cases. However, it is recommended to find a more descriptive name wherever
    possible to improve the readability and maintainability of the code.

对于 Perl 程序

  • 上述大多数 C 指南都适用。

  • 我们尽量支持 Perl 5.8.1 及更高版本(“use Perl 5.008001”)。

  • 强烈建议使用 strict 和 use warnings。

  • 除非使用它们使结果更容易理解,否则不要过度使用语句修饰符。

    1. do something …​ do_this() unless (condition);

    2. do something else …​

      is more readable than:
    3. do something …​ unless (condition) { do_this(); }

    4. do something else …​

      *only* when the condition is so rare that do_this() will be almost
      always called.
  • 我们尽量避免在 "if ()" 条件中赋值。

  • 如果您需要该功能,请学习并使用 Git.pm。

对于 Python 脚本

  • 我们遵循 PEP-8 (https://peps.pythonlang.cn/pep-0008/)。

  • 至少,我们的目标是与 Python 2.7 兼容。

  • 如果所需的库不限制我们使用 Python 2,我们也会尽量与 Python 3.1 及更高版本兼容。

程序输出

We make a distinction between a Git command's primary output and
output which is merely chatty feedback (for instance, status
messages, running transcript, or progress display), as well as error
messages. Roughly speaking, a Git command's primary output is that
which one might want to capture to a file or send down a pipe; its
chatty output should not interfere with these use-cases.
As such, primary output should be sent to the standard output stream
(stdout), and chatty output should be sent to the standard error
stream (stderr). Examples of commands which produce primary output
include `git log`, `git show`, and `git branch --list` which generate
output on the stdout stream.
Not all Git commands have primary output; this is often true of
commands whose main function is to perform an action. Some action
commands are silent, whereas others are chatty. An example of a
chatty action commands is `git clone` with its "Cloning into
'<path>'..." and "Checking connectivity..." status messages which it
sends to the stderr stream.
Error messages from Git commands should always be sent to the stderr
stream.

错误消息

  • 不要用句号结束单句错误消息。

  • 不要将第一个单词大写,仅仅因为它是一条消息中的第一个单词(“unable to open %s”,而不是“Unable to open %s”)。 但是“SHA-3 not supported”是可以的,因为第一个单词大写的原因不是因为它位于句子的开头,而是因为即使该单词出现在句子的中间,它也会以大写字母拼写。

  • 首先说明错误是什么(“cannot open %s”,而不是“%s: cannot open”)。

  • 将错误的主题放在一对单引号中,例如 die(_("unable to open '%s'"), path)

  • 除非有充分的理由不这样做,否则来自 porcelain 命令的错误消息应标记为可翻译的,例如 die(_("bad revision %s"), revision)

  • 来自 plumbing 命令的错误消息有时用于机器消费,不应标记为可翻译的,例如 die("bad revision %s", revision)

  • BUG("message") 用于向开发人员传达特定错误,因此不应翻译。

外部可见名称

  • 对于配置变量名,请遵循现有约定

    1. 节名称指示受影响的子系统。

    2. 子节名称(如果有)指示要设置值的无界事物集合中的哪一个。

    3. 变量名描述了调整此旋钮的效果。

      The section and variable names that consist of multiple words are
      formed by concatenating the words without punctuation marks (e.g. `-`),
      and are broken using bumpyCaps in documentation as a hint to the
      reader.
      When choosing the variable namespace, do not use variable name for
      specifying possibly unbounded set of things, most notably anything
      an end user can freely come up with (e.g. branch names).  Instead,
      use subsection names or variable values, like the existing variable
      branch.<name>.description does.

编写文档

Most (if not all) of the documentation pages are written in the
AsciiDoc format in *.adoc files (e.g. Documentation/git.adoc), and
processed into HTML and manpages (e.g. git.html and git.1 in the
same directory).
The documentation liberally mixes US and UK English (en_US/UK)
norms for spelling and grammar, which is somewhat unfortunate.
In an ideal world, it would have been better if it consistently
used only one and not the other, and we would have picked en_US
(if you wish to correct the English of some of the existing
documentation, please see the documentation-related advice in the
Documentation/SubmittingPatches file).
In order to ensure the documentation is inclusive, avoid assuming
that an unspecified example person is male or female, and think
twice before using "he", "him", "she", or "her".  Here are some
tips to avoid use of gendered pronouns:
  • 最好简洁明了地描述抽象中的功能。 例如:

    --short

    以短格式发出输出。

    and avoid something like these overly verbose alternatives:
    --short

    使用它以短格式发出输出。

    --short

    您可以使用它以短格式获取输出。

    --short

    一个喜欢较短输出的用户可以……

    --short

    如果一个人和/或程序想要较短的输出,他/她/他们/它可以……

    This practice often eliminates the need to involve human actors in
    your description, but it is a good practice regardless of the
    avoidance of gendered pronouns.
  • 当坚持这种风格变得很尴尬时,在与假设的用户交谈时,最好使用“你”,在讨论程序可能如何对用户做出反应时,可以使用“我们”。 例如:

    You can use this option instead of `--xyz`, but we might remove
    support for it in future versions.
    while keeping in mind that you can probably be less verbose, e.g.
    Use this instead of `--xyz`. This option might be removed in future
    versions.
  • 如果您仍然需要引用第三人称单数的示例人物,您可以求助于“单数 they”以避免“he/she/him/her”,例如:

    A contributor asks their upstream to pull from them.
    Note that this sounds ungrammatical and unnatural to those who
    learned that "they" is only used for third-person plural, e.g.
    those who learn English as a second language in some parts of the
    world.
    Every user-visible change should be reflected in the documentation.
    The same general rule as for code applies -- imitate the existing
    conventions.

标记

Literal parts (e.g. use of command-line options, command names,
branch names, URLs, pathnames (files and directories), configuration and
environment variables) must be typeset as verbatim (i.e. wrapped with
backticks):
  `--pretty=oneline`
  `git rev-list`
  `remote.pushDefault`
  `http://git.example.com`
  `.git/config`
  `GIT_DIR`
  `HEAD`
  `umask`(2)
An environment variable must be prefixed with "$" only when referring to its
value and not when referring to the variable itself, in this case there is
nothing to add except the backticks:
  `GIT_DIR` is specified
  `$GIT_DIR/hooks/pre-receive`
Word phrases enclosed in `backtick characters` are rendered literally
and will not be further expanded. The use of `backticks` to achieve the
previous rule means that literal examples should not use AsciiDoc
escapes.
  Correct:
     `--pretty=oneline`
  Incorrect:
     `\--pretty=oneline`
Placeholders are spelled in lowercase and enclosed in
angle brackets surrounded by underscores:
  _<file>_
  _<commit>_
If a placeholder has multiple words, they are separated by dashes:
  _<new-branch-name>_
  _<template-directory>_
When needed, use a distinctive identifier for placeholders, usually
made of a qualification and a type:
  _<git-dir>_
  _<key-id>_
Git's Asciidoc processor has been tailored to treat backticked text
as complex synopsis. When literal and placeholders are mixed, you can
use the backtick notation which will take care of correctly typesetting
the content.
  `--jobs <n>`
  `--sort=<key>`
  `<directory>/.git`
  `remote.<name>.mirror`
  `ssh://[<user>@]<host>[:<port>]/<path-to-git-repo>`

作为副作用,反引号括起来的占位符可以正确排版,但不建议使用这种样式。

概要语法

The synopsis (a paragraph with [synopsis] attribute) is automatically
formatted by the toolchain and does not need typesetting.
A few commented examples follow to provide reference when writing or
modifying command usage strings and synopsis sections in the manual
pages:
Possibility of multiple occurrences is indicated by three dots:
  <file>...
  (One or more of <file>.)
Optional parts are enclosed in square brackets:
  [<file>...]
  (Zero or more of <file>.)
An optional parameter needs to be typeset with unconstrained pairs
  [<repository>]
--exec-path[=<path>]
(Option with an optional argument.  Note that the "=" is inside the
brackets.)
[<patch>...]
(Zero or more of <patch>.  Note that the dots are inside, not
outside the brackets.)
Multiple alternatives are indicated with vertical bars:
  [-q | --quiet]
  [--utf8 | --no-utf8]
Use spacing around "|" token(s), but not immediately after opening or
before closing a [] or () pair:
  Do: [-q | --quiet]
  Don't: [-q|--quiet]
Don't use spacing around "|" tokens when they're used to separate the
alternate arguments of an option:
   Do: --track[=(direct|inherit)]
   Don't: --track[=(direct | inherit)]
Parentheses are used for grouping:
  [(<rev>|<range>)...]
  (Any number of either <rev> or <range>.  Parens are needed to make
  it clear that "..." pertains to both <rev> and <range>.)
[(-p <parent>)...]
(Any number of option -p, each with one <parent> argument.)
git remote set-head <name> (-a|-d|<branch>)
(One and only one of "-a", "-d" or "<branch>" _must_ (no square
brackets) be provided.)
And a somewhat more contrived example:
  --diff-filter=[(A|C|D|M|R|T|U|X|B)...[*]]
  Here "=" is outside the brackets, because "--diff-filter=" is a
  valid usage.  "*" has its own pair of brackets, because it can
  (optionally) be specified only when one or more of the letters is
  also provided.
A note on notation:
 Use 'git' (all lowercase) when talking about commands i.e. something
 the user would type into a shell and use 'Git' (uppercase first letter)
 when talking about the version control system and its properties.
If some place in the documentation needs to typeset a command usage
example with inline substitutions, it is fine to use +monospaced and
inline substituted text+ instead of `monospaced literal text`, and with
the former, the part that should not get substituted must be
quoted/escaped.
scroll-to-top