设置和配置
获取和创建项目
基本快照
分支与合并
共享和更新项目
检查和比较
打补丁
调试
电子邮件
外部系统
服务器管理
指南
管理
底层命令
- 2.50.1 无更改
-
2.50.0
2025-06-16
- 2.49.1 无更改
-
2.49.0
2025-03-14
- 2.48.1 → 2.48.2 无更改
-
2.48.0
2025-01-10
- 2.47.1 → 2.47.3 无更改
-
2.47.0
2024-10-06
- 2.46.2 → 2.46.4 无变化
-
2.46.1
2024-09-13
- 2.45.1 → 2.46.0 无变化
-
2.45.0
2024-04-29
- 2.43.2 → 2.44.4 无变化
-
2.43.1
2024-02-09
-
2.43.0
2023-11-20
- 2.42.2 → 2.42.4 无更改
-
2.42.1
2023-11-02
-
2.42.0
2023-08-21
- 2.41.1 → 2.41.3 无更改
-
2.41.0
2023-06-01
- 2.39.1 → 2.40.4 无更改
-
2.39.0
2022-12-12
- 2.38.3 → 2.38.5 无更改
-
2.38.2
2022-12-11
- 2.38.1 无更改
-
2.38.0
2022-10-02
- 2.37.1 → 2.37.7 无更改
-
2.37.0
2022-06-27
- 2.36.1 → 2.36.6 无更改
-
2.36.0
2022-04-18
- 2.35.1 → 2.35.8 无更改
-
2.35.0
2022-01-24
- 2.33.1 → 2.34.8 无变化
-
2.33.0
2021-08-16
- 2.32.1 → 2.32.7 无变更
-
2.32.0
2021-06-06
- 2.28.1 → 2.31.8 无更改
-
2.28.0
2020-07-27
- 2.27.1 无更改
-
2.27.0
2020-06-01
- 2.25.1 → 2.26.3 无更改
-
2.25.0
2020-01-13
- 2.22.2 → 2.24.4 无变化
-
2.22.1
2019-08-11
-
2.22.0
2019-06-07
- 2.20.1 → 2.21.4 无更改
-
2.20.0
2018-12-09
- 2.19.3 → 2.19.6 无更改
-
2.19.2
2018-11-21
- 2.17.1 → 2.19.1 无变化
-
2.17.0
2018-04-02
- 2.14.6 → 2.16.6 无变化
-
2.13.7
2018-05-22
- 2.12.5 无更改
-
2.11.4
2017-09-22
- 2.10.5 无更改
-
2.9.5
2017-07-30
-
2.8.6
2017-07-30
- 2.5.6 → 2.7.6 无变更
-
2.4.12
2017-05-05
-
2.3.10
2015-09-28
- 2.2.3 无变更
-
2.1.4
2014-12-17
-
2.0.5
2014-12-17
此信息专用于 Git 项目
请注意,如果您打算为 Git 项目本身做贡献,此信息才与您相关。它绝不是普通 Git 用户的必读内容。
和其他项目一样,我们也有一些代码规范。对于 Git 而言,一些粗略的规则如下:
-
最重要的是,我们从不说“这在 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
和esac
行的case arm
缩进深度相同,如下所示case "$variable" in pattern1) do this ;; pattern2) do that ;; esac
-
重定向操作符前应有空格,但其后不应有空格。换句话说,写 echo test >"$file" 而不是 echo test> $file 或 echo test > $file。请注意,尽管 POSIX 不要求对变量中的重定向目标加双引号(如上所示),但我们的代码这样做是因为某些版本的 bash 在没有引号的情况下会发出警告。
(incorrect) cat hello > world < universe echo hello >$world
(correct) cat hello >world <universe echo hello >"$world"
-
我们偏好使用 $( … ) 进行命令替换;与 `` ` `` 不同,它可以正确嵌套。它本应是 Bourne 从一开始就指定的方式,但可惜并非如此。
-
如果你想找出某个命令是否在用户的 $PATH 中可用,你应该使用 type
,而不是 which 。which 的输出无法被机器解析,并且其退出代码在不同平台之间不可靠。 -
我们使用符合 POSIX 的参数替换,并避免 bash 特有功能;即
-
我们使用 ${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 都支持它之前(值得注意的是,AT&T Research 的 ksh 尚未支持它),没有人开始使用“local”。
-
某些版本的 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:
-
自 2007 年左右,通过 2b6854c863a,我们一直在使用无法在加载时计算的初始化器元素。例如:
const char *args[] = { "constant", variable, NULL };
-
自 2012 年初,通过 e1327023ea,我们一直在使用枚举定义,其最后一个元素后跟一个逗号。这与以逗号结尾的数组初始化器一样,可用于在末尾添加新标识符时减少补丁的噪声。
-
自 2017 年年中,通过 cbc0f81d,我们一直在使用结构体(struct)的指定初始化器(例如 "struct t v = { .val = a };")。
-
自 2017 年年中,通过 512f41cf,我们一直在使用数组的指定初始化器(例如 "int array[10] = { [5] = 2 }")。
-
自 2021 年初,通过 765dc168882,我们一直在使用可变参数宏,主要用于类似 printf 的跟踪和调试宏。
-
自 2021 年末,通过 44ba10d6,我们已在 for 循环中声明变量 "for (int i = 0; i < 10; i++)"。
New C99 features that we cannot use yet:
-
size_t
的printf()
参数使用 %z 和 %zu(其中 %z 用于 POSIX 特定的ssize_t
)。相反,你应该使用printf("%"PRIuMAX, (uintmax_t)v)
。目前我们所依赖的 MSVC 版本支持 %z,但 MinGW 使用的 C 库不支持。 -
在结构体初始化中,像 ".a.b = *c*" 这样的简写形式已知会导致较旧的 IBM XLC 版本出错,请改用 ".a = { .b = *c* }"。请参阅 33665d98 (reftable: make assignments portable to AIX xlc v12.01, 2022-03-28)。
-
-
变量必须在块的开头、第一个语句之前声明(即 -Wdeclaration-after-statement)。鼓励在声明结束和块中的第一个语句之间留一个空行。
-
空指针应写为 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 进行显式比较。例如,要验证计数数组
已初始化但没有元素,请写: 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/ 之外,必须是 <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 核心命令集中尚未使用的脚本语言(除非你的命令与其明确分离,例如将 random-scm-X 仓库转换为 Git 的导入器)。
-
当我们将
对传递给函数时,我们应该尝试按此顺序传递。 -
使用 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”)。
-
强烈推荐使用
use strict
和use warnings
。 -
不要过度使用语句修饰符,除非使用它们能使结果更容易理解。
-
do something … do_this() unless (condition);
-
do something else …
is more readable than:
-
do something … unless (condition) { do_this(); }
-
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 commands)的错误消息应标记为可翻译,例如
die
(_
("bad
revision
%s"
),
revision
)。 -
来自底层命令(plumbing commands)的错误消息有时是为了机器消费,不应标记为可翻译,例如
die
("bad
revision
%s",
revision
)。 -
BUG("message")
用于向开发者传达特定错误,因此不应翻译。
外部可见名称
-
对于配置变量名,请遵循现有约定
-
章节名称指示受影响的子系统。
-
子章节名称(如果有的话)指示要为无限集合中的哪个项设置值。
-
变量名描述了调整此旋钮的效果。
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.
-
如果你仍然需要指代第三人称单数的示例人物,你可以使用“singular 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>_
字符也用下划线包围:LF、CR、CR/LF、NUL、EOF
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.