設定和配置
獲取和建立專案
基本快照
分支與合併
共享和更新專案
檢查和比較
打補丁
除錯
電子郵件
外部系統
伺服器管理
指南
管理
底層命令
- 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.python.org/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.