章節 ▾ 第二版

A3.1 附錄 C:Git 命令 - 設定與配置

在本書中,我們介紹了數十個 Git 命令,並努力以敘述的方式逐步引入更多命令。然而,這導致命令的使用示例分散在全書中。

在本附錄中,我們將回顧本書中提到的所有 Git 命令,並大致按其用途分組。我們將簡要介紹每個命令的功能,然後指出在本書的何處可以找到其用法。

設定和配置

有兩個命令使用頻率很高,從 Git 的首次呼叫到日常調整和引用,它們是 confighelp 命令。

git config

Git 有數百種預設行為。對於其中許多行為,你可以告訴 Git 預設以不同的方式執行它們,或設定你的偏好。這包括從告訴 Git 你的姓名到特定的終端顏色偏好或你使用的編輯器。此命令將從多個檔案讀取並寫入,因此你可以全域性設定值或針對特定倉庫進行設定。

git config 命令在本書的幾乎每個章節中都有使用。

首次 Git 設定中,我們使用它來指定我們的姓名、電子郵件地址和編輯器偏好,甚至在開始使用 Git 之前。

Git 別名中,我們展示瞭如何使用它建立可擴充套件為長選項序列的快捷命令,這樣你就無需每次都輸入它們。

變基中,我們使用它使 --rebase 成為你執行 git pull 時的預設選項。

憑證儲存中,我們使用它來設定 HTTP 密碼的預設儲存。

關鍵字擴充套件中,我們展示瞭如何為進出 Git 的內容設定塗抹(smudge)和清理(clean)過濾器。

最後,Git 配置的幾乎所有內容都專門介紹此命令。

git config core.editor commands

伴隨你的編輯器中的配置說明,許多編輯器可以按如下方式設定

表 4. core.editor 配置命令的詳盡列表
編輯器 配置命令

Atom

git config --global core.editor "atom --wait"

BBEdit (macOS,帶有命令列工具)

git config --global core.editor "bbedit -w"

Emacs

git config --global core.editor emacs

Gedit (Linux)

git config --global core.editor "gedit --wait --new-window"

Gvim (Windows 64 位)

git config --global core.editor "'C:\Program Files\Vim\vim72\gvim.exe' --nofork '%*'" (另請參閱下面的註釋)

Helix

git config --global core.editor "hx"

Kate (Linux)

git config --global core.editor "kate --block"

nano

git config --global core.editor "nano -w"

Notepad (Windows 64 位)

git config core.editor notepad

Notepad++ (Windows 64 位)

git config --global core.editor "'C:\Program Files\Notepad++\notepad++.exe' -multiInst -notabbar -nosession -noPlugin" (另請參閱下面的註釋)

Scratch (Linux)

git config --global core.editor "scratch-text-editor"

Sublime Text (macOS)

git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl --new-window --wait"

Sublime Text (Windows 64 位)

git config --global core.editor "'C:\Program Files\Sublime Text 3\sublime_text.exe' -w" (另請參閱下面的註釋)

TextEdit (macOS)

git config --global core.editor "open --wait-apps --new -e"

Textmate

git config --global core.editor "mate -w"

Textpad (Windows 64 位)

git config --global core.editor "'C:\Program Files\TextPad 5\TextPad.exe' -m" (另請參閱下面的註釋)

UltraEdit (Windows 64 位)

git config --global core.editor Uedit32

Vim

git config --global core.editor "vim --nofork"

Visual Studio Code

git config --global core.editor "code --wait"

VSCodium (VSCode 的自由/開源二進位制檔案)

git config --global core.editor "codium --wait"

WordPad

git config --global core.editor "'C:\Program Files\Windows NT\Accessories\wordpad.exe'"

Xi

git config --global core.editor "xi --wait"

注意

如果你的 Windows 64 位系統上安裝了 32 位編輯器,程式將安裝在 C:\Program Files (x86)\ 中,而不是像上表所示的 C:\Program Files\

git help

git help 命令用於顯示 Git 自帶的任何命令的所有文件。雖然我們在此附錄中大致概述了大多數更常用的命令,但要獲取每個命令的所有可能選項和標誌的完整列表,你始終可以執行 git help <command>

我們在獲取幫助中介紹了 git help 命令,並在設定伺服器中展示瞭如何使用它來查詢有關 git shell 的更多資訊。

scroll-to-top